1. 程式人生 > >多個git賬號之間的切換

多個git賬號之間的切換

做過很多遍了,卻總是記不住,這回從頭來描述一下。

注意clone的時候  有2種  https和SSH  是有區別的

  1. 使用https方式的git地址非常直接(https://xxx.oschina.net/xxx.git),基本上什麼都不需要配置,不管是git bash還是tortoisegit都能完美使用,但是每次需要連線遠端伺服器時,都要提示我輸入使用者名稱與密碼,非常不爽;
  2. 使用ssh方式的git地址非常爽快([email protected]:xxxx/xxx.git),不需要輸入密碼,但是需要配置。

介紹

所謂多個git賬號,可能有兩種情況:

  • 我有多個github的賬號,不同的賬號對應不同的repo,需要push的時候自動區分賬號

  • 我有多個git的賬號,有的是github的,有的是bitbucket的,有的是單位的gitlab的,不同賬號對應不同的repo,需要push的時候自動區分賬號

這兩種情況的處理方法是一樣的,分下面幾步走:

處理

  • 先假設我有兩個賬號,一個是github上的,一個是公司gitlab上面的。先為不同的賬號生成不同的ssh-key

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_work -c [email protected]

    然後根據提示連續回車即可在~/.ssh目錄下得到id_rsa_work和id_rsa_work.pub兩個檔案,id_rsa_work.pub檔案裡存放的就是我們要使用的key

    ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -c [email protected]

    然後根據提示連續回車即可在~/.ssh目錄下得到id_rsa_github和id_rsa_github.pub兩個檔案,id_rsa_gthub.pub檔案裡存放的就是我們要使用的key

  • 把id_rsa_xxx.pub中的key新增到github或gitlab上,這一步在github或gitlab上都有幫助,不再贅述

  • 編輯 ~/.ssh/config,設定不同的git 伺服器對應不同的key

1
2
3
4
5
6
7
8
9
10
11
12
# Default github user(
[email protected]
),注意User項直接填git,不用填在github的使用者名稱
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_github # second user([email protected]) # 建一個gitlab別名,新建的帳號使用這個別名做克隆和更新 Host 172.16.11.11 HostName 172.16.11.11 User work IdentityFile ~/.ssh/id_rsa_work

編輯完成後可以使用命令 ssh -vT [email protected] 看看是不是採用了正確的id_rsa_github.pub檔案

這樣每次push的時候系統就會根據不同的倉庫地址使用不同的賬號提交了

  • 從上面一步可以看到,ssh區分賬號,其實靠的是HostName這個欄位,因此如果在github上有多個賬號,很容易的可以把不同的賬號對映到不同的HostName上就可以了。比如我有A和B兩個賬號, 先按照步驟一生成不同的key檔案,再修改~/.ssh/config 內容應該是這樣的。
1
2
3
4
5
6
7
8
9
10
11
12
# Default github user([email protected]),注意User項直接填git,不用填在github的使用者名稱
Host A.github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_github_A
# second user([email protected])
# 建一個gitlab別名,新建的帳號使用這個別名做克隆和更新
Host A.github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_github_B

同時你的github的repo ssh url就要做相應的修改了,比如根據上面的配置,原連線地址是:

[email protected]:testA/gopkg.git

那麼根據上面的配置,就要把github.com換成A.github.com, 那麼ssh解析的時候就會自動把testA.github.com 轉換為 github.com,修改後就是

[email protected]:testA/gopkg.git

直接更改 repo/.git/config 裡面的url即可

這樣每次push的時候系統就會根據不同的倉庫地址使用不同的賬號提交了

一些題外話

我有一個repo,想要同時push到不同的倉庫該如何設定?

很簡單, 直接更改 repo/.git/config 裡面的url即可,把裡面對應tag下的url增加一個就可以了。例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[remote "GitHub"]
    url = [email protected]:elliottcable/Paws.o.git
    fetch = +refs/heads/*:refs/remotes/GitHub/*
[branch "Master"]
    remote = GitHub
    merge = refs/heads/Master
[remote "Codaset"]
    url = [email protected]:elliottcable/paws-o.git
    fetch = +refs/heads/*:refs/remotes/Codaset/*
[remote "Paws"]
    url = [email protected]:Paws/Paws.o.git
    fetch = +refs/heads/*:refs/remotes/Paws/*
[remote "Origin"]
    url = [email protected]:Paws/Paws.o.git
    url = [email protected]:elliottcable/paws-o.git

上面這個立即就是有4個遠端倉庫,不同的tag表示不同的遠端倉庫,最後的Origin標籤寫法表示預設push到github和codaset這兩個遠端倉庫去。當然,你可以自己隨意定製tag和url

我有一個github的repo,clone沒有問題,push的時候總是報錯:error: The requested URL returned error: 403 while accessing xxx

這個問題也困擾了我一段時間,後來發現修改 repo/.git/config 裡面的url,把https地址替換為ssh就好了。

例如

url=https://[email protected]/derekerdmann/lunch_call.git

替換為

url=ssh://[email protected]/derekerdmann/lunch_call.git