SSH 多账号管理仓库

2021-02-20  本文已影响0人  沙人Q

具体可参考:https://docs.github.com/cn/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

  1. 用 Mac 自带工具,生成 ssh Key
    命令行:ssh-keygen -t rsa -C xxxx@github.com
    rsa 加密算法
    -C 创建账号
ssh-keygen -t rsa -C xxxx@qq.com
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/temp/.ssh/id_rsa): /Users/temp/.ssh/id_rsa_xiaobu
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/temp/.ssh/id_rsa_temp.
Your public key has been saved in /Users/temp/.ssh/id_rsa_temp.pub.
The key fingerprint is:
SHA256:9apFioweHNwSwFnL/GKYBlZxJrnbuzumFh3YZK09xII xxxx@github.com
The key's randomart image is:
+---[RSA 3072]----+
| ..*=oo          |
|  +E+= +         |
|..  @ =   .      |
|.. * B o . .     |
|  + X + S . .    |
| . = O . o .     |
|    = + . o      |
|   o =   o       |
|  ..+o+ .        |
+----[SHA256]-----+
  1. 打开.ssh 文件中 pub 文件复制,访问https://github.com/settings/keys 点击添加 New SSH keys,把复制内容填充到下图中
    截屏2021-02-20 16.33.08.png
    名称自定义 ,点击 Add SSH key 即可
  2. 将 ssh 密钥添加到 ssh-agent中
ssh-add ~/.ssh/id_rsa_temp
  1. 测试:ssh -T git@github.com
Hi  xxxx(名称)! You've successfully authenticated, but GitHub does not provide shell access.
  1. 配置 config 进入 Mac 根目录的.ssh 文件中 打开 config
#个人账号
Host person.github.com
HostName github.com
User person_用户名
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_person

#公司账号
Host company.github.com
HostName github.com
User  company_用户名
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_company

6.替换项目仓库地址

git remote -v

origin  https://github.com/xxxx.git (fetch)
origin  https://github.com/xxxx.git (push)
git remote remove origin
git remote add origin git@company.github.com:xxxx.git 

地址中 company.github.com 对应的是 config 文件中 host 后面内容

注意:

  1. 如果发现项目不能提交时:
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
git@github.com: Permission denied (publickey)
执行 : ssh-add ~/.ssh/id_rsa_temp
  1. 出现 "The current branch master has no upstream branch "
    问题原因:本地分支没有和远程分支关联起来
    解决方案:git push --set-upstream origin master (master代表远程分支名称)
上一篇下一篇

猜你喜欢

热点阅读