Git多用户配置
两个仓库,github上一个,gitlab上一个,用户名与密码不同,而且两个项目都需要处理。这时候就需要配置多个用户,进行不同仓库的操作。
1. 清除之前的全局配置
# 列出全局用户名,邮箱配置
git config --global --list
# 重置用户名和邮箱
git config --global --unset user.name
git config --global --unset user.email
2. 生成新的秘钥
删除旧的秘钥: 旧的秘钥存放在用户目录的.ssh文件夹中,删除其中的 id_rsa
、id_rsa.pub
之类的公钥和密钥文件。
生成新的秘钥:
ssh-keygen -t rsa -C "123456@qq.com"
ssh-keygen -t rsa -C "78910@qq.com"
3. 添加ssh key
将公钥配置到github和gitlab中。
4. 将私钥添加到本地
ssh-add ~/.ssh/id_rsa_github
ssh-add ~/.ssh/id_rsa_gitlab
报错:Could not open a connection to your authentication agent.解决方法
ssh-agent bash # 执行此方法
5. 管理秘钥
在.ssh文件夹需要创建秘钥配置文件config,用来配置不同的秘钥连接不同的仓库。
Host github
HostName github.com
User binfenshengdai
IdentityFile ~/.ssh/id_rsa_github
Host gitlab
HostName gitlab.mygitlab.com
User test
IdentityFile ~/.ssh/id_rsa_gitlab
- Host: 仓库的别名,可以随意取名
- HostName: 仓库网站的域名
- User: 仓库上面的用户名
- IdentityFile: 私钥的绝对路径
验证连接是否成功:
ssh -T git@github
ssh -T git@gitlab
6. 仓库配置
我们需要为每个仓库单独配置用户名信息,假设我们要配置 github 的某个仓库,进入该仓库后,执行:
git config --local user.name "username"
git config --local user.email "username@qq.com"