多个git账户和项目切换

参考: http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/


一、最原始的切换方法。

gitlab和github项目切换时, github上多个项目账户不同时,都需要切换用户。

如下面的切换方法:

git config --global user.email [[email protected]]
git config --global user.name [myname]

但是这个方法需要来回切换,实在是太麻烦。



二、每个项目设置单独的user.name 和 user.email

可以为每个项目设置非全局的user.name 和 user.email, 在命令行环境,进入Git项目所在目录,执行下面的命令:

$ git config user.name nickname#将用户名设为nickname
$ git config user.email [email protected] #将用户邮箱设为[email protected]

 这只是解决同一个网站(公用一个rsa密码)上多个用户的问题。如果不同项目有不同的托管库网站(如gitlab和github),还需要另外的设置。


三、不同的托管库设置不同的rsa密码

如果不同项目有不同的托管库和ssh,则可通过设置ssh key的config 文件来解决。

1. 在~/.ssh下用ssh-keygen 生成多个rsa文件。 -t后的参数为自定义文件名。 如:

ssh-keygen -t rsa_github
ssh-keygen -t rsa_gitlab

将生成的公钥保存到相关托管库

新建或修改 ~/.ssh/config

#config文件通过host区分不同的托管库
#Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/rsa_github


Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/rsa_gitlab

要设定每个项目的地址时,只要指定@的地址为相应的Host
如:
git remote add origin [email protected]:nickname/testing.git
git clone [email protected]:nick/testing.git
至此,系统就能自动选择对应的私钥文件。

2,3两个步骤结合,就能无需切换用户。再多的用户和再多的托管库,也可以以上面的方法慢慢扩展,一次配置无需切换。



猜你喜欢

转载自blog.csdn.net/jdk137/article/details/17579837
今日推荐