本地配置不同git账户,可以对不同仓库、不同账号、不同平台进行管理 例如需要配置github和gitlab两个平台,但两个平台的邮箱不一致
例如,我本身已为gitlab配置了一个global账户,那么操作如下
1、先设置仓库的远程地址
进入新建的仓库目录,打开gitbase
git remote set-url origin git@github.com:username/repo.git
其中,origin 是该仓库的 Git 远程仓库别名,git@github.com:username/repo.git 是该仓库的 SSH URL
2、本地生成一个新的SSH
ssh-keygen -t rsa -f ~/.ssh/id_rsa_account1 -C "your.email@example.com"
其中,id_rsa_account1 是新生成的 SSH 密钥文件名,可以根据自己的需要修改;your.email@example.com 是账号对应的邮箱地址,应该和账号在 Git 仓库中绑定的邮箱一致。
然后就设置name和email,不加 --global就是对当前的目录下的仓库进行设置
git config user.name "Your Username for Account 1"
git config user.email "your.email@example.com"
3、在.ssh添加config文件
将内容复制到config文件中,对指定的网站映射到对应的密钥
# github
Host github.com
HostName github.com
User name
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_account1
// 测试 SSH 连接是否成功,如果显示 Hi username! You've successfully authenticated...,则说明添加成功。
ssh -T git@github.com
// 或者直接push或pull,也可以测试有没有成功