前言
在开发的过程中,经常会遇到这样的状况:需要在一台电脑上同时使用两个甚至多个 git 账号,负责不同的用途,比如:一个用来写个人项目,一个用来写公司的项目。为此我们需要为不同的账号生成不同的密钥,那对这些不同的账号和不同的密钥,我们该怎么处理呢?
SSH配置
娜娜项目网每日更新创业和副业项目
网址:nanaxm.cn 点击前往娜娜项目网
站 长 微 信: nanadh666
取消全局设置的用户名和邮箱
$ git config --global --unset user.name
$ git config --global --unset user.email
生成私钥和公钥
cd ~/.ssh && mkdir -pv {github,company}
$ ssh-keygen -t rsa -C "youremail@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Administrator/.ssh/id_rsa):/Users/Administrator/.ssh/github/id_rsa_github
# 在回车提示中输入完整路径,如:/Users/Administrator/.ssh/github/id_rsa_github
#文件命名后,按2次回车,密码为空
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
$ ssh-keygen -t rsa -C "youremail@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Administrator/.ssh/id_rsa):/Users/Administrator/.ssh/company/id_rsa_company
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
如果用户家目录中没有 .ssh 目录请自行创建 在这里我创建了两个目录 和 ,分别用来存储不同项目的密钥,进行分类管理
New SSH key
把 ~/.ssh//.pub 的内容添加到的SSH keys中把 ~/.ssh//.pub 的内容添加到公司的SSH keys中
新密钥添加到SSH agent中
添加新密钥到SSH agent,因为默认只读取authenticatedusers是什么用户,0,11,-1,users可以删除吗(users是什么意思)-蘑菇号,https://www.mooogu.cn/xinwen/266695.html,为了让SSH识别新的私钥,需将其添加到SSH agent中:
$ ssh-add -K ~/.ssh/github/id_rsa_github
$ ssh-add -K ~/.ssh/company/id_rsa_company
如果出现Could not open a to your agent的错误authenticatedusers是什么用户,0,11,-1,users可以删除吗(users是什么意思)-蘑菇号,https://www.mooogu.cn/xinwen/266695.html,就试着用以下命令:
$ ssh-agent bash
$ ssh-add -K ~/.ssh/github/id_rsa_github
使用 ssh-add -l 查看 ssh key 的设置
修改 文件
$ vim ~/.ssh/config
Host *
KexAlgorithms +diffie-hellman-group1-sha1
# default: myfirst
Host github.com
HostName github.com
User myfirst
PreferredAuthentications publickey
IdentityFile ~/.ssh/github/id_rsa_github1
# mysecond
Host mysecond.github.com
HostName github.com
User mysecond
PreferredAuthentications publickey
IdentityFile ~/.ssh/github/id_rsa_github2
Host company.com
HostName company.com
User company
PreferredAuthentications publickey
IdentityFile ~/.ssh/company/id_rsa_company
其规则就是:从上至下读取的内容,在每个Host下寻找对应的私钥。这里将 SSH仓库地址中的替换成新建的Host别名如:,那么原地址是::test/.git,替换后应该是::test/.git.
测试连通性
$ ssh -T git@github.com
Hi youremail! You've successfully authenticated, but GitHub does not provide shell access.
项目测试
初始化项目a
$ cd ~/a
$ git init
$ echo "myfirst" > README.md
$ git add README.md
$ git config user.name "myfirst"
$ git config user.email "myfirst@gmail.com"
$ git remote add github git@github.com:myfirst/test.git
$ git push -u github master
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 213.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:myfirst/test.git
* [new branch] master -> master
Branch master set up to track remote branch master from github.
初始化项目b
$ cd ~/b
$ git init
$ echo "mysecond" > README.md
$ git add README.md
$ git config user.name "mysecond"
$ git config user.email "mysecond@gmail.com"
$ git remote add github git@mysecond.github.com:mysecond/test.git
$ git push -u github master
Counting objects: 3, done.
Writing objects: 100% (3/3), 218 bytes | 218.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To mysecond.github.com:mysecond/test.git
* [new branch] master -> master
Branch master set up to track remote branch master from github.
娜娜项目网每日更新创业和副业项目
网址:nanaxm.cn 点击前往娜娜项目网
站 长 微 信: nanadh666