同一台电脑使用多个sshkey
参考链接:http://higrid.net/c-art-github_share_ssh.htm
http://yijiebuyi.com/blog/f18d38eb7cfee860c117d629fdb16faf.html
1、使用命令新建个ssh-key
tingdeMacBook-Pro:ContinuousIntegration ting$ ssh-keygen -t rsa -C 'xxxx@163.com'
2、注意命令运行时输入新的文件名:
tingdeMacBook-Pro:ContinuousIntegration ting$ ssh-keygen -t rsa -C 'xxxx@163.com' Generating public/private rsa key pair. Enter file in which to save the key (/Users/ting/.ssh/id_rsa): /Users/ting/.ssh/id_rsa_xxxx163_github Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /Users/ting/.ssh/id_rsa_xxxx163_github. Your public key has been saved in /Users/ting/.ssh/id_rsa_xxxx163_github.pub. The key fingerprint is:……
3、默认SSH只会读取id_rsa,所以为了让SSH识别新的私钥,需要将其添加到SSH agent
ssh-add ~/.ssh/id_rsa_xxxx163_github
Identity added: /Users/ting/.ssh/id_rsa_xxxx163_github (/Users/ting/.ssh/id_rsa_xxxx163_github)
4、完成以上步骤后在~/.ssh目录创建config文件,该文件用于配置私钥对应的服务器。内容如下:
# Default github user(first@mail.com) Host github.com HostName github.com User git IdentityFile C:/Users/username/.ssh/id_rsa # second user(second@mail.com) Host github-second HostName github.com User git IdentityFile C:/Users/username/.ssh/id_rsa_second
5、Host随意即可,方便自己记忆,后续在添加remote是还需要用到。 配置完成后,在连接非默认帐号的github仓库时,远程库的地址要对应地做一些修改,比如现在添加second帐号下的一个仓库test,则需要这样添加:
git remote add test git@github-second:second/test.git #并非原来的git@github.com:second/test.git
注意: 1、HostName一定要填写为github.com,否则会报错
ssh: Could not resolve hostname github_tinkle: nodename nor servname provided, or not known
fatal: Could not read from remote repository.
2、Host为用于连接时的所写的host名。
3、second处所填写的为自己在git上的账号名
4、github根据配置文件的user.email来获取github帐号显示author信息,所以对于多帐号用户一定要记得将user.email改为相应的email(second@mail.com)。
备注:个人记录
1、clone:git clone git@github.com:TinkleSun/hello-world.git
2、建立远程库:git remote add origin git@github.com:TinkleSun/markdown_repository.git
3、报错'Write failed: Broken pipe' 原因是网络不好连接超时。。我改用vpn后就不再报这个错误了
4、第一次使用push时,git push -u origin master,会报错
tingdeMacBook-Pro:markdown_repository ting$ git push -u origin master Warning: Permanently added the RSA host key for IP address '192.30.252.130' to the list of known hosts. To git@github.com:TinkleSun/markdown_repository.git ! [rejected] master -> master (fetch first) error: failed to push some refs to 'git@github.com:TinkleSun/markdown_repository.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. tingdeMacBook-Pro:markdown_repository ting$ git pull Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts. warning: no common commits remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:TinkleSun/markdown_repository * [new branch] master -> origin/master Merge branch 'master' of github.com:TinkleSun/markdown_repository There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
采用git pull 依然报错,而后尝试 git pull origin master,此后再尝试push成功
tingdeMacBook-Pro:markdown_repository ting$ git pull origin master Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts. From github.com:TinkleSun/markdown_repository * branch master -> FETCH_HEAD Merge made by the 'recursive' strategy. README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md hello world! tingdeMacBook-Pro:markdown_repository ting$ git push -u origin master Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (5/5), 531 bytes | 0 bytes/s, done. Total 5 (delta 0), reused 0 (delta 0) To git@github.com:TinkleSun/markdown_repository.git 9e3f010..941de30 master -> master Branch master set up to track remote branch master from origin.