每个项目单独配置 git 用户

时间:2022-11-13 13:36:49
git多账号登陆问题

设置git全局设置:

git config --global user.name "your_name" 
git config --global user.email  "your_email"

需要取消git的全局设置:

git config --global --unset user.name
git config --global --unset user.email

针对每个项目,单独设置用户名和邮箱,设置方法如下:

git config user.name "your_name" git config user.email "your_email"
说白了,也就是进入到你的git项目相对根目录下,然后执行git config设置记录

SSH配置(转的 未测试)

我看了很多中文博客,发现讲的都不太清楚,还是在*上,找了一个问题解决我的疑惑:http://*.com/questions/14689788/multiple-github-accounts-what-values-for-host-in-ssh-config
解决方法总结如下:
(1) 我现在有两个git项目,使用的用户名分别是A/B,用的邮箱分别是C/D
(2) 在~/.ssh目录下,使用 ssh-keygen -C "your_email" -t rsa 生成公私秘钥,命名分别为 id_rsa_first, id_rsa_second,公钥的内容需要分别上传到git项目的服务器上
(3) 在~/.ssh目录下创建config文件,进行相应配置:

#第一个git项目账号 Host first HostName test.com #这里需要用真实的项目检出hostname,为了项目安全,我这里随意写的 User A IdentityFile ~/.ssh/id_rsa_first  #第二个git项目账号 Host second HostName test2.com Port 1334 User B IdentityFile ~/.ssh/id_rsa_second

(4) 新建git项目检出目录,我发现很多同学出问题,在于git项目没有初始化

mkdir project && cd project git init git config user.name "A" git config user.email "C"

相应的第二个项目也参照上面的指令进行初始化设置
(5)检出服务端项目代码,这里需要注意,使用.ssh目录下的host代替真实的hostname,这样才能让git识别出来

git remote add first git@first:A/project.git

如果使用的是repo,也是同样操作

repo init -u ssh://A@first -b branch

(6)push的时候,push到对应的Host即可

first项目中: git push fist master