# Git连接多个远程仓库

一、 在MAC的终端下输入以下命令,查看密匙

ls ~/.ssh

如果存在id_rsa和id_rsa.pub,说明已存在一对公钥/私钥。

二、 创建新的密匙,并指定密匙的名称,例如id_rsa_github

ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "yourmail@xxx.com"

操作完成后,该目录会多出id_rsa_github和id_rsa_github.pub两个文件。

三、 在~/.ssh/文件夹下创建一个config文件

cd ~/.ssh/
touch config
vim config

四、 编写config文件,配置不同的仓库指向不同的密钥文件

# 第一个账号,默认使用的账号
Host *github.com
HostName ssh.github.com
User yuhongjing
IdentityFile ~/.ssh/id_rsa_github
# 第二个账号
Host baidu  # second为前缀名,可以任意设置
HostName relay.baidu-int.com
User yuhongjing
IdentityFile ~/.ssh/id_rsa_icode

五、 查看SSH密匙的值,分别添加到对应的Git账户中

cat id_rsa_icode.pub
cat id_rsa_github.pub

六、 清空本地SSH缓存,添加新的SSH密匙到SSH agent中

ssh-add -D
ssh-add id_rsa_icode
ssh-add id_rsa_github

七、 确认新密匙是否添加成功

ssh-add -l

八、 测试ssh链接

ssh -T git@github.com
ssh -T git@icode.baidu.com
# xxx! You’ve successfully authenticated, but GitHub does not provide bash access.
# 出现上述提示,连接成功

九、 取消git全局用户名/邮箱的设置,设置独立的用户名/邮箱

# 取消全局 用户名/邮箱 配置
git config --global --unset user.name
git config --global --unset user.email
# 进入项目文件夹,单独设置每个repo 用户名/邮箱
git config user.email "xxxx@xx.com"
git config user.name "xxxx"

十、 查看git项目的配置

git config --list

十一、 命令行进入项目目录,重建origin

git remote rm origin
# 远程仓库地址,注意Host名称
git remote add origin git@second.github.com:githubUserName/repName.git
git remote -v # 查看远程

十二、 Git命令大全

最近更新时间: 2020/10/10 18:15:40