Git 一台电脑配置多个账户

1. 配置全局用户

##用户名和邮箱名和账号名没有必然相关性,可以不一样
##我用github的账号设置了user.email,然而gitlab照样可以push数据
##首次clone数据时有要求输入该网站的账号和密码,可以理解git的配置是git的账号和昵称
git config --global user.name "zhangsan"
git config --global user.email "zhangsan@163.com" 

查看全局配置

$ git config -l
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
http.sslbackend=openssl
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
credential.helper=manager
user.name=zhangsan
user.email=zhangsan@163.com

2. 生成账户1的密钥

$ ssh-keygen -t rsa -C "zhangsan@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):  ## 直接敲回车,就会生成id_rsa文件
Enter passphrase (empty for no passphrase): ## 密码为空,不然每次提交代码时都让你输入密码,也很烦
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
SHA256:BaavA6jaa2qa61VnSReLfxgT1x+4+bx7hP39gXGryaE zhangsan@163.com
The key's randomart image is:
+---[RSA 2048]----+
|        oo .. .  |
|       o..=  o . |
|      .o =.   + .|
|   .  ..+.+  o . |
|  . .. +So . .o+ |
| .  ..o.  .   =o+|
|.  .  o      o ++|
|.+o    .    o +.=|
|X=o.       E + .=|
+----[SHA256]-----+

3. 生成账户2的密码

$ ssh-keygen -t rsa -C "laoyu@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key 
## 注意,这里要输入保存的文件名,一定要和id_rsa不一样
(/c/Users/Administrator/.ssh/id_rsa): id_rsa_laoyu 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_laoyu.
Your public key has been saved in id_rsa_laoyu.pub.
The key fingerprint is:
SHA256:BaavA6jaa2qa61VnSReLfxgT1x+4+bx7hP39gXGryaE laoyu@163.com
The key's randomart image is:
+---[RSA 2048]----+
|        oo .. .  |
|       o..=  o . |
|      .o =.   + .|
|   .  ..+.+  o . |
|  . .. +So . .o+ |
| .  ..o.  .   =o+|
|.  .  o      o ++|
|.+o    .    o +.=|
|X=o.       E + .=|
+----[SHA256]-----+

这时在你的用户目录下,会有以下几个文件
这里写图片描述

4. 将用户加入ssh

$ ssh-add ~/.ssh/id_rsa_laoyu
Could not open a connection to your authentication agent.
## 遇到了错误 Could not open a connection to your authentication agent.

## 解决方法 输入
ssh-agent bash

## 然后重新输入
ssh-add ~/.ssh/id_rsa_laoyu
## 出现如下信息表示添加成功
Identity added: /c/Users/Administrator/.ssh/id_rsa_laoyu (/c/Users/Administrator/.ssh/id_rsa_laoyu)

## 继续添加
ssh-add ~/.ssh/id_rsa
Identity added: /c/Users/Administrator/.ssh/id_rsa (/c/Users/Administrator/.ssh/id_rsa)

4. 配置config文件

现在打开文件夹,到git密钥存放的位置~/.ssh,新建文件config没有后缀名
然后用编辑器打开,输入以下内容

# 该配置用于公司工作
# Host 服务器别名,这个别名以后在clone、pull、push时替代具体的IP或域名
Host work
# HostName 服务器ip地址或机器名
HostName 10.0.210.152
# User连接服务器的用户名
User git
# IdentityFile 密匙文件的具体路径
IdentityFile C:/Users/Administrator/.ssh/id_rsa  


# 该配置用于个人
# Host 服务器别名
Host laoyu
# HostName 服务器ip地址或机器名
HostName 10.0.210.152
# User连接服务器的用户名
User git
# IdentityFile 密匙文件的具体路径  
IdentityFile C:/Users/Administrator/.ssh/id_rsa_laoyu 

完成后,可以进行验证,出现如下信息标识成功。注意:你需要咋gitlab上进行公钥的添加,然后在如下进行验证时,会返回你在gitlab上注册的账号名称

ssh -T git@work
Welcome to GitLab, zhangsan!

ssh -T git@laoyu
Welcome to GitLab, laoyu!

## 注意,两次Welcome to GitLab之后的名字不一样,如果一样,说明没配对

5. 使用

5.1 clone

## 单账号时,clone方式
git clone git@10.0.210.152:mti/xxx.git
## 多账号时,clone方式
git clone git@work:mti/xxx.git

## 注意,git@后边不再是具体的IP或者域名,而是我们在config文件里,配置的服务器别名Host work

5.2 pull & push

push 和 pull也和上边多账号方式一样,要使用host的别名,而不再是直接使用IP

git remote add origin git@work:mti/xxx.git

git pull origin master

git push origin master
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页