您的位置:首页 > 其它

git 在一台机器上配置多个账户

2014-09-18 20:02 190 查看
前提:

必须知道如何配置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 “xxx@xxx.com”

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

remote.origin.url=git@github.com:testaccount/test.git

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

branch.master.remote=origin

branch.master.merge=refs/heads/master

user.name= xxxxxx

user.email=xxx@xxx.com

注意这一行remote.origin.url=git@github.com:testaccount/test.git,这里的 github.com表示config文件里的hostname,其实他并不是hostname而是一个别名,这样解释:

git@别名: testaccount/test.git解析的路径为 git@github.com: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 = git@github.com:testaccount/test.git

fetch = +refs/heads/*:refs/remotes/origin/*

[branch "master"]

remote = origin

merge = refs/heads/master

[user]

name = testaccount

email = xxx@xxx.com

我们只需把 url = git@github.com:testaccount/test.git改为 url = git@github2.com: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

remote.origin.url=git@github2.com:testaccount/test.git

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

branch.master.remote=origin

branch.master.merge=refs/heads/master

user.name= testaccount

user.email=xxx@xxx.com

大功告成,你可以push代码了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: