|
| 1 | +# Multiple SSH Keys settings for different github account |
| 2 | + |
| 3 | +如果你對這個真的很陌生,建議看影片:smile: |
| 4 | + |
| 5 | +* [Youtube Tutorial - Multiple SSH Keys settings for different github account](https://youtu.be/gDxG-4tF7B8) |
| 6 | + |
| 7 | +## 前言 |
| 8 | + |
| 9 | +有時候我們可能會遇到這種情境,就是同一台電腦上同時需要有多個 github 帳號,為什麼呢? |
| 10 | + |
| 11 | +當你工作的電腦和你自己的 side project 開發都在同一台電腦的時候,你就需要這篇文章的幫忙 |
| 12 | + |
| 13 | +了 :kissing_smiling_eyes: |
| 14 | + |
| 15 | +相信我,你不會希望用公司的帳號推 commit 到自己的 side project 上:sweat_smile: |
| 16 | + |
| 17 | +## 教學 |
| 18 | + |
| 19 | +第一步,先產生 ssh key,如果你不知道怎麼產生, |
| 20 | + |
| 21 | +可參考 [Generating a new SSH key](https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/) 或是 [github基本教學 - 從無到有](https://www.youtube.com/watch?v=py3n6gF5Y00), |
| 22 | + |
| 23 | +打開你的 Git Bash, |
| 24 | + |
| 25 | +```cmd |
| 26 | +ssh-keygen -t rsa -b 4096 -C "blue555236sa56@gmail.com" |
| 27 | +``` |
| 28 | + |
| 29 | +( 請將信箱換成自己的 ) |
| 30 | + |
| 31 | +這裡的重點是記得自行輸入新的 rsa 名稱,像是下圖的 `/c/Users/twtrubiks/.ssh/id_rsa_rubik` 這樣, |
| 32 | + |
| 33 | +這樣才不會覆蓋舊有的 rsa, |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +接下來,你的 `.ssh` 路徑底下會多出你剛剛建立的檔案 |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +再把你的 `id_rsa_rubik.pub` key 貼到你的 gihub 上, |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +通常你新增成功的時候,信箱會收到一封信。 |
| 46 | + |
| 47 | +再輸入下方指令 |
| 48 | + |
| 49 | +```cmd |
| 50 | +ssh-agent -s |
| 51 | +``` |
| 52 | + |
| 53 | +上面這個指令一定要執行,如果跳過這個步驟,就會出現以下錯誤, |
| 54 | + |
| 55 | +Could not open a connection to your authentication agent. |
| 56 | + |
| 57 | +所以請記得一定要執行。 |
| 58 | + |
| 59 | +在 `.ssh` 目錄底下建立一個名稱為 `config` 的檔案 ( 檔名就是 `config`,副檔名不用填 ), |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +下方是一個範例, |
| 64 | + |
| 65 | +```config |
| 66 | +# github - twtrubiks |
| 67 | + Host github.com |
| 68 | + HostName github.com |
| 69 | + IdentityFile ~/.ssh/id_rsa |
| 70 | + IdentitiesOnly yes |
| 71 | + User twtrubiks |
| 72 | +
|
| 73 | +# github - blue-rubiks |
| 74 | + Host blue.github.com |
| 75 | + HostName github.com |
| 76 | + IdentityFile ~/.ssh/id_rsa_rubik |
| 77 | + IdentitiesOnly yes |
| 78 | + User blue-rubiks |
| 79 | +``` |
| 80 | + |
| 81 | +可用以下指令測試,如果顯示出自己的使用者名稱,代表設定成功 |
| 82 | + |
| 83 | +```cmd |
| 84 | +ssh -T git@github.com |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +```cmd |
| 90 | +ssh -T git@blue.github.com |
| 91 | +``` |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +ya :heart_eyes: 我們設定成功惹~ |
| 96 | + |
| 97 | +如果有問題,也可以用 debug 模式看問題出在哪裡,通常是 `config` 輸入有誤 |
| 98 | + |
| 99 | +```cmd |
| 100 | +ssh -vT git@github.com |
| 101 | +``` |
| 102 | + |
| 103 | +接下來先和大家說明一下, |
| 104 | + |
| 105 | +```cmd |
| 106 | +git config --global user.name "blue-rubiks" |
| 107 | +git config --global user.email "blue555236sa56@gmail.com" |
| 108 | +``` |
| 109 | + |
| 110 | +`--global` 顧名思義就是全域的意思,如果沒輸入,就代表是目錄底下的 repo 會生效, |
| 111 | + |
| 112 | +現在我們 clone 一個 repo 來玩玩看 (你可以自己建立一個) |
| 113 | + |
| 114 | +```cmd |
| 115 | +git clone git@github.com:blue-rubiks/github-ssh-test.git |
| 116 | +``` |
| 117 | + |
| 118 | +接著,切換到目錄資料夾底下,輸入以下指令 |
| 119 | + |
| 120 | +```cmd |
| 121 | +git config user.name "blue-rubiks" |
| 122 | +git config user.email "blue555236sa56@gmail.com" |
| 123 | +``` |
| 124 | + |
| 125 | +注意,這邊沒加上 `--global`,所以只有該目錄底下的 repo 會生效, |
| 126 | + |
| 127 | +我們找到 repo 中的 `.git` 資料夾 , 再找到 `config` 檔案,內容大致如下 |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +主要就是修改第 9 行成第 10 行,修改為你設定的 `Host`, |
| 132 | + |
| 133 | +最後,可以安心 commit 以及 push 了。 |
| 134 | + |
| 135 | +如果你真的聽不懂我在說什麼,建議看影片說明,我會帶大家操作一波 :laughing: |
| 136 | + |
| 137 | +## 執行環境 |
| 138 | + |
| 139 | +* Win 10 |
| 140 | + |
| 141 | +## Reference |
| 142 | + |
| 143 | +* [Multiple SSH Keys settings for different github account](https://gist.github.com/jexchan/2351996) |
| 144 | + |
| 145 | +## Donation |
| 146 | + |
| 147 | +文章都是我自己研究內化後原創,如果有幫助到您,也想鼓勵我的話,歡迎請我喝一杯咖啡:laughing: |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +[贊助者付款](https://payment.opay.tw/Broadcaster/Donate/9E47FDEF85ABE383A0F5FC6A218606F8) |
| 152 | + |
| 153 | +## License |
| 154 | + |
| 155 | +MIT licens |
0 commit comments