[转]如何切换多个GitHub账号
轉載說明:感謝原作者!如有侵權,請聯系我刪除,謝謝!
原文鏈接:https://www.jianshu.com/p/0ad3d88c51f4
如何切換多個GitHub賬號
1 前言
很慚愧做了幾年開發,剛剛使用Git。入職的時候使用公司郵箱注冊的GitHub賬號,后來使用私人郵箱又注冊一個。為了解決同一系統上多個賬號切換的問題,查了一些資料,在此總結一下。
注:當前操作系統是Windows10,shell工具是PowerShell, Git版本為Git for Windows-2.9.2-64bit。
2 本地配置
遇到的第一個問題是Author。每次查看提交記錄時,總會看到Author的用戶名和郵箱。如果使用兩個不同的賬戶當然希望看到不同的Author提交。Git把信息都存儲在本地的config文件中,包括后面要說到的本地憑證存儲也和config有關,所以在此有必要說一下。
Git共有三個級別的config文件,分別是system、global和local。在當前環境中,分別對應%GitPath%\mingw64\etc\gitconfig文件、$home\.gitconfig文件和%RepoPath%\.git\config文件。其中%GitPath%為Git的安裝路徑,%RepoPath%為某倉庫的本地路徑。所以system配置整個系統只有一個,global配置每個賬戶只有一個,而local配置和git倉庫的數目相同,并且只有在倉庫目錄才能看到該配置。
2.1 查看配置
# list git all config about user. tips: D:\test\TestUser is a dictionary of Repo PS D:\test\TestUser> git config --list | sls user user.email=SystemUser.com user.name=SystemUser user.name=GlobalUser user.email=GlobalUser.com user.name=LocalUser user.email=LocalUser.com以上可見,篩選出的user.name和user.email就是前面提到的Author信息。此時,上面提到的三個級別都配置了user信息。當git commit時,Author信息依次讀取local、global和system的配置,如果找到則不再繼續讀取。其他配置的讀取順序也是如此。
2.2 更改配置
更改local級別的user.name(本地Repo中的config):
# set user.name in local config and query. tips: D:\test\TestUser is a dictionary of Repo PS D:\test\TestUser> git config --local user.name NinputerWonder PS D:\test\TestUser> git config --local user.name NinputerWonder依此類推,可以更改global和system的配置。其他配置的更改和讀取操作類似。
接下來會用到上面類似的操作更改本地憑證存儲。
3 使用https協議
GitHub支持https和ssh協議連接。當執行類似命令git clone https://github.com/NinputerWonder/xxx.git后,在此倉庫中的上傳下載均采用https協議;類似git clone git@github.com:NinputerWonder/xxx.git則使用ssh協議。首先討論使用https協議。
當提交代碼后,向服務器push的時候,這時git會提示輸入用戶名和密碼。輸入正確之后,git會有多種策略存儲本地憑證,以免下次再輸入賬戶信息。
切換到`%GitPath%\mingw64\libexec\git-core路徑下,可以查看本地存儲工具:
PS C:\Program Files\Git\mingw64\libexec\git-core> (ls).Name | sls credential git-credential-manager.exe git-credential-store.exe git-credential-wincred.exe git-credential.exe以上列出當前支持的三種存儲輔助工具,即mananger、wincred和store。Git可以指定輔助工具(通過配置credential.helper),用來存儲本地憑證。
3.1 manager
若安裝Git時安裝了GitGUI,自動會在system級別中設置credential.helper為manager。并且不配置所處級別(system、global或者local)如何,一旦設置了manager,都優先使用該方式。
查看不同級別的credential.helper
PS D:\test\TestUser> git config --global credential.helper manager PS D:\test\TestUser> git config --local credential.helper store若本地沒有存儲憑證,第一次push的時候,會彈出窗口,要求輸入用戶名密碼(圖1)。
輸入并驗證成功后并將其存儲至Windows的憑據管理器中(圖2)。
圖 2 Windows憑據管理器中記錄的信息(通過manager工具保存)
當再次在本機push的時候,會直接讀取憑據管理器的中賬戶信息。經個人測試,除非手動點擊“編輯”或者“刪除”,否則無法更改賬戶。
3.2 wincred
設置credential.helper,如wincred
# query current credential.helper config PS D:\test\TestUser> git config --list | sls credential.helper credential.helper=manager credential.helper=store # remove credential setting in local config PS D:\test\TestUser> git config --local --remove-section credential # change credential.helper to wincred in global config PS D:\test\TestUser> git config --global credential.helper wincred # query current credential.helper config. only wincred left PS D:\test\TestUser> git config --list | sls credential.helper credential.helper=wincred上面刪除了local中的credential.helper配置并且設置了global中的改配置,所以會使用后者。
如果本地沒有賬戶信息,當push時會提示輸入。如果輸入正確也會記錄在Windows的憑據管理器中(圖3)
除此之外,該工具還可以使用命令行管理本地存儲。
查看本地記錄賬號(protocol和host是輸入,注意空行,其余為輸出) PS D:\test\TestUser> git credential-wincred get protocol=https host=github.com
username=NinputerWonder
password=123456
刪除本地賬號(以下全部為輸入,注意空行)
PS D:\test\TestUser> git credential-wincred erase protocol=https host=github.com username=NinputerWonder password=123456after erase, the account was removed
PS D:\test\TestUser> git credential-wincred get
protocol=https
host=github.com
同時Windows的憑據管理器中該賬戶信息也被刪除。
添加賬號(以下全部為輸入,注意空行)
PS D:\test\TestUser> git credential-wincred store protocol=https host=github.com username=NinputerWonder password=123456
同時,該賬號信息出現在Windows的憑據管理器中。
但是,經個人測試, 如果同一個host使用wincred存儲多份憑證,只會有一個賬戶生效。
3.3 store
將當前local級別的credential.helper設置成store(過程和前面類似),此時的存儲方式變成了store。如果本地沒有存儲賬號信息,當push時輸入正確信息,將此保存至home目錄下的.git-credentials文件,并且以明文存儲,內容如下:
https://NinputerWonder:123456@github.com
官方文檔指出可以使用類似如下參數的形式指定存儲上述信息文件。
git config --global credential.helper store --file=~/cred.txt但經個人測試并未生效,不過可以直接修改config文件來達到目的,如:
[credential]
helper = store --file=D:/credential/cred1.txt
再次push,便會將賬戶信息存至D:/credential/cred1.txt中。若想清除賬戶,刪除該文件即可。
3.4 小結
綜上,因為store使用明文保存賬戶信息,存在安全隱患。使用manager和wincred方式比較安全,但不能滿足多賬戶(GitHub賬戶)切換(除非刪除重錄)。
使用store方式時,配置每個repository的local級別credential.helper,并且通過--file配置不同的文件,這樣每個repository可以讀取獨立的賬戶信息,可滿足需求。
4 使用ssh協議
當使用ssh協議時,不需要像上面存儲賬戶信息,但是需要在客戶端和服務器上分別存儲成對的private key和public key,校驗成功,方可與服務器通信(包括git clone命令)。
4.1 生成Key
使用如下命令生成key
PS D:\ssh> ssh-keygen -t rsa -b 4096 -C "NinputerWonder@outlook.com" Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/kingw/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/kingw/.ssh/id_rsa. Your public key has been saved in /c/Users/kingw/.ssh/id_rsa.pub. The key fingerprint is: SHA256:lmAq9yVuN2u6r4zffwfadSsxn78nwJghg/StRGSRk0 NinputerWonder@outlook.com The key's randomart image is: +---[RSA 4096]----+ |o=o=E+ | |A ++o* | |o+. * o | | ... = . . | |o o = + S | |.. = B + + | | . . B o | | * +.o | | ..**+ | +----[SHA256]-----+如果不顯式指定目錄和文件名,生成文件public key文件為$home/.ssh/ id_rsa.pub, private key為$home/.ssh/ id_rsa,并且自動添加至ssh-agent。
4.2 將public key上傳至服務器
登陸個人賬戶,打開Setting->SSH and GPB keys->New SSH Key,將public key上傳至GItHub服務器(圖4)。
此時,在本地 git clone git@github.com:NinputerWonder/xxx.git即可下載Repository。
4.3 配置多賬戶
假設上一個賬戶為私人賬戶,現在為公司賬戶創建key,并且將public key添加至GitHub服務器。
ssh-keygen -t rsa -b 4096 -f $home/.ssh/company -C "wdwangtw@gmail.com"將新生成的private key添加至ssh-agent。
PS D:\test> ssh-agent bash kingwtw@DESKTOP-IIKCT85 /d/test $ ssh-add ~/.ssh/rsa-company Identity added: /c/Users/kingwtw/.ssh/company (/c/Users/kingwtw/.ssh/company)接下來創建ssh的config文件(注意該config文件和前面提到的配置文件不是同一類型),該文件位置為$home/.ssh/config。內容如下:
Host myhost personal User NinputerWonder HostName github.com IdentityFile ~/.ssh/id_rsaHost myhost company
User wdwangtw
HostName github.com
IdentityFile ~/.ssh/company
不難看出,其格式為
Host myhost USER_HOST ;USER_HOST為自定義host名字,如上面的personal和company User USER_NAME ;USER_NAME為自定義名稱 HostName SERVER_HOST ;SERVER_HOST為實際服務器host,此時為GitHub IdentityFile PRIVATE_KEY ;PRIVATE_KEY為本地key當再次clone一個新Repos時,如果其ssh地址為git@github.com:wdwangtw/xxx.git,使用git clone git@company:wdwangtw/TestUserwd.git即可clone到本地(注意github.com換成了自定義的company),并且push時也不用輸入任何驗證。
4.4 小結
使用ssh協議時,通過配置key的方式,從clone到push每一步都會進行key的校驗,使用起來更加安全可靠。
總結
使用https和ssh協議都可以做到多個GitHub賬號在同一系統分別工作。使用https時,通過配置local的credential.helper為store,并且指定本地存儲文件的方式達到目的。但是store方式明文存儲用戶信息,容易泄露賬號。使用ssh協議時,如果更換機器,需要將本地private key拷貝至新系統,或者在新系統上生成新的key,并且把新生成的private key添加到GitHub后才能正常使用。
后記
以上為個人在當前環境下測試,某些官方提供的命令并未生效。如果讀者發現不正確或者不明確的地方,歡迎指正。
參考文檔
1 Customizing Git
2 GItHub Help - SSH
3 http://www.cnblogs.com/BeginMan/p/3548139.html
總結
以上是生活随笔為你收集整理的[转]如何切换多个GitHub账号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ROS 常用命令汇总(不定期更新)
- 下一篇: 覃超-算法训练营 学习方法分享[1] 如