# 在 Github 上使用 SSH key push 靜態網頁+展示 ###### tags:`Github`,`架站` --- [TOC] --- ## 前言 由於 Github 從 2021/08/13 開始停止提供輸入帳密的服務,所以現在只能用 SSH 金鑰了 QQ 再加上看到網路上的教學似乎都有點舊了,就來重寫一個 - ## 建立 SSH key ### 1. cd 到想要上傳到 Github的資料夾 ``` ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ``` 打了這行後,就會出現這些: ``` Generating public/private rsa key pair. Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again] ``` 簡單一點的話,3 個問題可以全部都按Enter跳過~ 接著,再繼續往下打 3 個指令 ### 2. 開啟 ssh-agent ``` eval "$(ssh-agent -s)" ``` ### 3. 將私鑰加入到 ssh-agent ``` ssh-add -K ~/.ssh/id_rsa ``` ### 4. 上傳公鑰 在 .ssh/ 資料夾下會分別有 id_rsa、id_rsa.pub 這兩個檔案,也就分別代表私鑰及公鑰,這一個步驟就是將 id_rsa.pub 也就是公鑰上傳到 Github 上。 ``` cat ~/.ssh/id_rsa.pub ``` 會出現一大串英文跟數字夾雜的公鑰,由 ssh-rsa開頭,自己的 email 結尾。接著就把整串都複製下來。 到 Github 網站,點選==Settings -> SSH and GPG keys -> New SSH Key==,把剛剛複製的一大串內容都貼上去,上面的標題可以自訂一個名稱,這樣就完成了! 這樣之後就不用再輸入帳密也能 push 檔案了~ ## 建立新的 repository :::info 這裡其實有兩種方法可以建立 repository,那我比較懶只介紹使用過的方法,想看另一種方法可以參考[這裡](https://eudora.cc/posts/45315/) ::: ### 1. 建立名為 username.github.io 的 repository 步驟: 打開 Github -> 點選右上角的頭像 -> Your repositories -> 右上角綠綠的 New 點下去之後,就會進入以下畫面 ![](https://i.imgur.com/3WwfizZ.png) 這時候username的地方記得設定成你在 Github 的 username! 接下來只要把靜態網頁丟到 Github 就大功告成! ## 上傳檔案 :::info 後面的指令也是在欲上傳資料夾中完成,所以前面的指令完成後可以接著打下面的部分! ::: ### 1. 初始化 ``` git init ``` 打完指令後,就會資料夾中一個多出一個檔案叫.git,會儲存git有關的所有資訊!但因為他是隱藏檔,所以打開資料夾是看不到的喔! > 如果想要將.git 刪除 > ``` > rm -r .git > ``` ### 2. 把檔案新增至 staging area - 只新增一個檔案 ``` git add "file name" ``` - 一次新增資料夾中所有檔案 ``` git add . ``` 打完指令就代表將檔案從紅色區域傳送到了藍色區域! ![](https://i.imgur.com/v35Mejt.png) 這時可以查看 git status,看看檔名是不是綠色的,如果有就代表成功上傳! ### 2. 把檔案新增至 local git repository ``` git commit -m "messeges" ``` - 假設我在"messeges"那邊打"first version",在上傳後會出現在檔名旁邊,算是備註的功能 ![](https://i.imgur.com/oGVMFXZ.png) ### 3. push 檔案至遠端 repository :::info 前面步驟都是在 local 端完成的,接下來要 push 至遠端 repo 才算完成上傳的動作! ::: - 設定遠端 repo ``` git remote add <local repo name> <your github repo> ``` 1. <local repo name> 是你的之後用來 push 和 clone 用的網址,通常是打 origin,當然要打別的名稱也是可以! 2. 注意這裡的 <your github repo> ,因為我們前面是選擇 SSH 的方式,所以要填的是 repo 的 SSH 網址喔! 步驟:your repo -> 綠綠的Code -> 選擇 SSH 網址再貼到指令上就行了! ![](https://i.imgur.com/hb1SCx2.png) > 如果遠端網址想要再重新修改 > ``` > git remote set-url <local repo name> <your github repo> > ``` > 查詢對 remote 的命名 > ``` > git remote -v > ``` ### 4. 最後一步! - 一般來說,寫<remote repo name>就行了,不用再另外寫<local repo name> ``` git push <remote address> <local repo name>:<git repo name> ``` > 由於 git 的本地分支預設是 master,如果沒有改變的話,<git repo name> 那邊要打 master 喔! 若最後出現結果顯示下圖的訊息,就代表你已經成功 push 檔案到遠端了! ![](https://i.imgur.com/dsEXwMd.png) 可以到 Github 上確認檔案有沒有上去!