如何連接多個GitHub帳戶

fish~~
Feb 1, 2021

Target: 使用 SSH 去連接 GitHub 帳戶

  1. 產生一個新的 SSH key
$cd ~/.ssh 
$ssh-keygen -t rsa -C "userName@address"

當成功建立, 在 /.ssh 裏面會有兩個檔案 “id_rsa_XXX” 和 “id_rsa_XXX.pub”.
(Key file 的名字是由用戶自己設定)

2. 在 GitHub 儲存 Public Key

github.com -> Settings -> SSH and GPG Keys -> New SSH Keys

id_rsa_XXX.pub 的內容複製貼上.

$pbcopy < ~/.ssh/id_rsa_XXX.pub // copy the file content(Mac)

3. 設定 ssh config

$vim ~/.ssh/config在 config file 裏輸入:
Host github-new
HostName github.com
User loginUserName
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_XXX

4. 測試連線

$ssh -T git@github-new
Hi loginUserName! You’ve successfully authenticated, but GitHub does not provide shell access.

這裡要 use the “git” user (from GitHub Docs ).

如果有問題需要debug可以加v.
$ssh -vT git@github-new

5. 在 local 加入 remote repository paths

$git remote add origin git@github-new:loginUserName/repoName.git

6. 設定 user 和 email

設定這個Repository的User資料
$git config user.email "xxx@xxx.com"
$git config user.name "loginUserName"

7. 可以進行 pull, commit, push…

--

--