This article uses the configuration of github.com account and git.oschina.net account to demonstrate step by step how to configure Git multi-account support in Windows environment, that is, to manage multiple ssh-keys on the same computer , and to have a better understanding of git.
Note: All commands in this article are executed in administrator mode, that is, open cmd, and the Git Bash client runs the program as an administrator.
1. Generate the private key and public key corresponding to github.com (the file address in this article is C:\Users\popfisher directory)
Execute the command ssh-keygen -t rsa -C email to create the sshkey corresponding to github, named id_rsa_github , password 123456
ssh-keygen -t rsa -C 774232122@qq.com
2. Produce the private key and public key of git.oschina.net in the same way (the email address can be the same or different, the same in this article)
Execute the command ssh-keygen -t rsa -C email to create the sshkey corresponding to github, named id_rsa_oschina, password 123456
ssh-keygen -t rsa -C 774232122@qq.com
3. Copy the file obtained above to the .ssh directory accessed by git by default (win10 is in the user directory, this article is C:\Users\popfisher.ssh)
In addition to the key file, the config file is manually generated in the following steps, and the known_hosts file is automatically generated later
4. Upload the public key corresponding to github and the public key corresponding to oschina to the server
The way GitHub adds SSH keys is shown in the following figure:
The way to add SSH key to git.oschina.net is shown in the following figure:
5. Create a config text file in the .ssh directory and complete the relevant configuration ( the most core place )
Each account is configured with a separate Host , each Host needs to take an alias, and each Host mainly configures two attributes , HostName and IdentityFile .
The name of the Host can be taken as the name you like, but this will affect git related commands, for example:
Host mygithub If this is defined, the command is as follows, that is, the name immediately after git@ is changed to mygithub
git clone git@ mygithub :PopFisher/AndroidRotateAnim .git
HostName 这个是真实的域名地址
IdentityFile 这里是id_rsa的地址
PreferredAuthentications 配置登录时用什么权限认证--可设为publickey,password publickey,keyboard-interactive等
User 配置使用用户名
config文件配置如下:
# 配置github.com
Host github.com
HostName github.com
IdentityFile C:\\Users\\popfisher\\.ssh\\id_rsa_github
PreferredAuthentications publickey
User username1
# 配置git.oschina.net
Host git.oschina.net
HostName git.oschina.net
IdentityFile C:\\Users\\popfisher\\.ssh\\id_rsa_oschina
PreferredAuthentications publickey
User username2
6. 打开Git Bash客户端(管理员身份运行)执行测试命令测试是否配置成功(会自动在.ssh目录生成known_hosts文件把私钥配置进去)
7. 测试成功之后就可以在电脑上同时使用git多多账号同时操作,互不影响了
clone github上的项目AndroidRotateAnim
打开github上AndroidRotateAnim项目,复制其对应的clone命令入下图所示
执行clone命令如下:
clone oschina.net上的项目AndroidDevTools
打开git.oschina.net上AndroidDevTools项目,复制其对应的clone命令入下图所示
执行clone命令如下:
学习心得
在使用git的过程中,一般都只关注单账号的情况吗,工作中的配置也是直接由自己的上级发一个配置文档之类的东西,然后对着搞一遍就行了,可是当自己真正有这个需求的时候,突然发现束手无策。我自己当时也属于这个情况,有一天在家里,突然发现自己需要一台电脑支持多个SSH key的时候才发现自己不知道怎么下手了,于是就下定决心自己动手研究一下。对于技术,还是要亲自动手实践,实践出来的东西才能形成自己的真理,才不那么容易忘记。
这篇文章是我基于Win10平台上的实践,由于知识有限,可能还是没有总结得很完整,也可能在其他平台上会遇到一些其他的问题,没有时间去研究了,如果读者有遇到什么问题,欢迎留言讨论,共同学习。