1. 程式人生 > >git 在一臺機器上配置多個賬戶

git 在一臺機器上配置多個賬戶

rac ssh xxxxxx pla 必須 account ack entity 設置

前提:
必須知道怎樣配置git賬戶,請參考git官方教程:https://help.github.com/articles/generating-ssh-keys
這個教程能教你怎樣生成ssh-key,以及怎樣加入ssh-key。
補充一點。怎樣設置user.name和user.email。命令例如以下:
1)設置局部的user.name和user.email
git config user.name “xxxxxx”
git config user.email [email protected]
2) 設置全局的user.name和user.email
git config --gloable user.name “xxxxxx”
git config –gloable user.email “”


第一步:

建一個新的github賬戶。名字為testaccount,

假設你已經有了。跳過此步(註:你之前已經有了一個老的賬戶了,假設沒有,請看“前提”先來一個賬戶)


第二步:
假設自己會生成和配置ssh-key。那麽要配其它賬戶首先要在生成一個ssh-key。當然新的ssh-key名稱要和之前的有所差別,默認的private key 名稱為id_rsa。新的key要換個名稱。比方id_rsa2。這樣生成一套key的名稱分別為id_rsa2和id_rsa2.pub。而默認的文件為id_rsa和id_rsa.pub

在github上建一個新的repository。當然是基於你的第二個賬戶testaccount的。比如名稱為test


第三步:

git clone下來


第四步:
然後要在.ssh文件夾下配置一下config文件(假設沒有,創建它),樣例例如以下:
# Default account
Host github.com
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa


# New account
Host github2.com
Hostname github.com
User git
IdentityFile ~/.ssh/id_rsa2


此時發現,這個配置看不懂啊,沒關系,下邊你能夠使用命令,在一個test文件夾下運行git config -l 命令查看配置。例如以下所看到的:
core.symlinks=false
core.autocrlf=false
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
mergetool.prompt=false
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
[email protected]
/* */:testaccount/test.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name= xxxxxx
[email protected]


[email protected]:testaccount/test.git,這裏的 github.com表示config文件中的hostname,事實上他並非hostname而是一個別名,這樣解釋:
[email protected]: testaccount/test.git解析的路徑為 [email protected]:testaccount/test.git而我配的default account的host和hostname剛好一樣,假設僅僅有一個賬戶的時候,它並不表示別名而是路徑。此時我們不須要配置config文件。我們設置config文件的目的是由於我們有兩套key,分別用在兩個repository,而我們須要分別指向這兩個key。簡單來說。我們是要通過host來找到key。即通過host映射到IdentityFile。


此時打開test文件夾下.get文件夾(隱藏文件夾)的config文件,內容例如以下:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
[remote "origin"]
url = [email protected]:testaccount/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
[user]
name = testaccount
email = [email protected]


我們僅僅需把 url = [email protected]:testaccount/test.git改為 url = [email protected]:testaccount/test.git,即使用了new account的host來配置。映射到了新的IdentityFile新的key,就可以保存文件再使用命令git config -l 查看配置例如以下:
core.symlinks=false
core.autocrlf=false
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
pack.packsizelimit=2g
help.format=html
http.sslcainfo=/bin/curl-ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
mergetool.prompt=false
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
[email protected]:testaccount/test.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name= testaccount
[email protected]


大功告成。你能夠push代碼了

git 在一臺機器上配置多個賬戶