展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

1台电脑关联多个远程git仓库

  • 参考

  • 删除.ssh目录下的id_rsa 和 id_rsa.pub

  • 生成第1对密钥

ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "xxx@xxx.com"
  • 生成第2对密钥
ssh-keygen -t rsa -f ~/.ssh/id_rsa_xxx_github -C "xxx@xxx.com"
  • 查看是否生成成功
$ ls ~/.ssh
id_rsa_xxx_github      id_rsa_github      known_hosts
id_rsa_xxx_github.pub  id_rsa_github.pub  known_hosts.old
  • 进入到 ~/.ssh/ 文件夹下创建一个 config 文件
$ touch config
$ vim config

# 编写如下
# 第一个账号,默认使用的账号
Host github.xxx.com
HostName github.xxx.com
User git
IdentityFile ~/.ssh/id_rsa_xxx_github
# 第二个账号
Host github.com  
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github
  • 将id_rsa_xxx_github.pub 和 id_rsa_github.pub的值填写到对应的远程仓库

  • 清空本地的 SSH 缓存,添加新的 SSH 密钥 到 SSH agent中
EAD+ychen224@CN-PF36VPKF MINGW64 ~/.ssh
$ eval `ssh-agent`
Agent pid 852

EAD+ychen224@CN-PF36VPKF MINGW64 ~/.ssh
$ ssh-add -D
All identities removed.

EAD+ychen224@CN-PF36VPKF MINGW64 ~/.ssh
$ ssh-add ~/.ssh/id_rsa_xxx_github
Identity added: /c/Users/ychen224/.ssh/id_rsa_xxx_github (xxx@xxx.com)

EAD+ychen224@CN-PF36VPKF MINGW64 ~/.ssh
$ ssh-add ~/.ssh/id_rsa_github
Identity added: /c/Users/ychen224/.ssh/id_rsa_github (xxx@xxx.com)
  • 验证是否添加成功
EAD+ychen224@CN-PF36VPKF MINGW64 ~/.ssh
$ ssh-add -l
3072 SHA256:xxx/xxx xxx@xxx.com (RSA)
3072 SHA256:xxx/xxx xxx@xxx.com (RSA)

EAD+ychen224@CN-PF36VPKF MINGW64 ~/.ssh
$ ssh -T git@github.com
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

EAD+ychen224@CN-PF36VPKF MINGW64 ~/.ssh
$ ssh -T git@github.xxx.com
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
  • 取消全局 用户名/邮箱 配置
$ git config --global --unset user.name
$ git config --global --unset user.email
  • 新建1个项目文件夹作为本地仓库,单独设置每个repo 用户名/邮箱
# 初始化
$ git init 
$ git config user.email "xxxx@xx.com"
$ git config user.name "xxxx"
  • 查看本地仓库配置
git config --list
  • 关联远程仓库
$ git remote rm origin
# 远程仓库地址,注意Host名称
$ git remote add origin git@xxx.github.com:githubUserName/repName.git
# 查看远程
$ git remote -v 
  • 切换分支,拉取项目
git branch -M main
git pull 
  • 报错
remote: Password authentication is not available for Git operations.
remote: You must use a personal access token or SSH key.
remote: See https://github.dxc.com/settings/tokens or https://github.dxc.com/settings/ssh
fatal: unable to access 'https://github.aaa.com/bbb/demo_repo.git/': The requested URL returned error: 403

# 解决方案:使用如下方式直接克隆
git clone git@github.aaa.com:bbb/demo_repo.git

posted @ 2022-07-13 14:22  DogLeftover  阅读(31)  评论(0编辑  收藏  举报