Git多帳號問題

12-30-2017

有一陣子沒更新文章,都忘記自己還有一個 Blog 空間,剛好最近碰到 Git 使用多帳號問題,順便把處理過程記錄下來(^◯^)
自己使用 Github 空間放 Blog 文章,公司則使用 Bitbucket 作為版控,總不可能傻傻的下一堆指令切換,光是輸入一堆指令就覺得麻煩,於是上網 google 找了一些文章,只要設置 Blog 使用的 Github 帳號跟 Bitbucket 用的帳號,就可以達到多帳號管理。

Git UI

設置 SSH

設置各自的 SSH-KEY ,假設我的 Github 跟 Bitbucket 使用者名稱為 blogPersonal 和 workComp ,帳號則各自對應 me@blogpersonal 和 me@workcomp

1
2
3
4
5
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "me@workcomp"
# 把這個 key 存成 id_rsa_workcomp
$ ssh-keygen -t rsa -C "me@blogpersonal"
# 把這個 key 存成 id_rsa_blogpersonal

產生後的 KEY 在~/.ssh/ 目錄下

  • id_rsa_blogpersonal
  • id_rsa_blogpersonal.pub
  • id_rsa_workcomp
  • id_rsa_workcomp.pub

Github 跟 Bitbucket 新增 SSH

到各自的空間把設置後的 SSH 新增上去,於同目錄下,複製 public key:

1
$ pbcopy < ~/.ssh/id_rsa_workcomp.pub

Github:

  • 登入你的帳號
  • 到 Account Settings
  • 點擊 SSH Keys ,點擊 Add SSH Key 按鈕
  • 在輸入框內貼入剛剛複製的 KEY 然後點擊 Add Key 即可

Bitbucket:

  • 登入你的帳號
  • 到 Bitbucket settings
  • Security 項目下點擊 SSH keys ,點擊 Add Key 按鈕
  • 在 Key 輸入框內貼入剛剛複製的 KEY 然後點擊 Add Key 即可

SSH 設定有疑義,請自行google或參閱Hexo安裝心得

配置 config 檔

接著只要在 ~/.ssh/ 目錄下產生 config 檔,這檔案用在設置私鑰對應各自的空間,建立 github.com 簡稱,使用這個簡稱做克隆和更新,簡稱可以依照個人喜好設定,只要
換成其他帳號
Host 是 github.com 的直接指到個人用的設定(KEY),當然也可以依照個人喜好把預設的換成其他帳號。

1
2
3
4
5
6
7
8
9
10
11
12
13
# Github 上的帳號
Host blogPersonal
HostName github.com
User blogPersonal
IdentityFile ~/.ssh/id_rsa_blogpersonal
IdentitiesOnly yes

# Bitbucket 上的帳號
Host workComp
HostName bitbucket.org
User workComp
IdentityFile ~/.ssh/id_rsa_workcomp
IdentitiesOnly yes

配置 Git 帳戶

單一帳號時使用全域很方便,在多帳號情況下沒有取消全域,只針對專案目錄設置區域 git config ,使用 git push 還是會發生 Permission denied (publickey) 錯誤,可以先確認目前區域設定 $ git config --list ,全域設定的確認方式 git config --list --global,我採用取消全域的方式,懶得一直確認區域 git config 是否有設置完成。

1
2
3
# 取消全域
$ git config --global --unset user.name
$ git config --global --unset user.email

專案目錄設置區域 git config

1
2
$ git config user.name "your_name"
$ git config user.email "your_email"

SSH 連線設定

config 檔寫完之後,接著測試是否連線OK?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 測試 Github 和 Bitbucket 連線
$ ssh -T blogPersonal
Hi blogPersonal! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T workComp
logged in as workComp.
You can use git or hg to connect to Bitbucket. Shell access is disabled.

# 無法連線
Permission denied (publickey).

# 確認 key 是否都加入了,如果沒有 key ,出現 The agent has no identities. 錯誤
$ ssh-add -l

# 添加 key ,ssh-add 在系統重啟後會有可能會丟失 key 的位置
$ ssh-add ~/.ssh/id_rsa_workcomp

新增 GIT REPO

在專案目錄下初始化 git

1
2
3
4
5
6
7
8
9
10
11
12
# 專案目錄下
$ mkdir Git_Hub/test-work && cd Git_Hub/test-work

# 隨便加個檔案
$ touch readme.md

$ git init
$ git add .
$ git commit -am "first commit"
$ git remote add origin git@workComp:workComp/test-work.git
$ git push origin master
Push 成功就算大功告成!工作帳號也是類似指令。

修改顯有 REPO

現有的專案的方式,修改專案下的 .git/config 檔案儲存著 git 的設定資訊

1
2
3
4
5
6
7
8
9
# 原本的 config是這樣的,現在我們改成
[remote "origin"]
url = git@github.com:workComp/existing-project.git
fetch = +refs/heads/*:refs/remotes/origin/*

# 改動的地方只有一個,把 host (github.com) 改成 work,這樣就可以了。
[remote "origin"]
url = git@workComp:workComp/existing-project.git
fetch = +refs/heads/*:refs/remotes/origin/*

Clone 遠端 REPO

Clone 遠端 REPO 的方式

1
2
3
4
5
6
7
8
9
10
$ cd ~/Git_Hub/
$ git clone git@workComp:userNmae/second_project.git
$ cd second_project/
$ git config user.name "your_name"
$ git config user.email "your_email"
$ touch readme.md
$ git status
$ git add readme.md
$ git commit -m "new file"
$ git push origin master

踩到的坑

上述設置都OK後 git push 到遠端,發生 Permission denied (publickey) 錯誤,解決方式如下:

  • 確認 SSH-KEY 是否添加到帳戶內,使用 $ ssh-add -l 指令
  • 確認 /User/.ssh/config 中有無添加 IndentityFile ~/.ssh/id_rsa_workcomp
  • 使用測試命令 ssh -v hg@bitbucket.org ,確認是否有錯誤訊息,如果無法正常連線,使用下方步驟
  • 使用 ssh-add ~/.ssh/id_rsa_workcomp 添加 key
  • ssh-add 添加還是有問題,請使用 ssh-add -K ~/.ssh/id_rsa_workcomp

參考文章:

SSH Keys with Multiple GitHub Accounts
git多用户管理
深入浅出 Git 权限校验
同時使用多個 Git 帳號