Git使用总结
Posted apescode
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git使用总结相关的知识,希望对你有一定的参考价值。
一、配置
- 记住密码
设置 -> git 编辑本地 .git/config 增加
[credential]
helper = store
1
[credential]
2
helper = store
保存,输入一次密码后第二次就会记住密码了
- 配置ssh支持多个git
步骤一 用ssh-keygen命令生成一组新的id_rsa和id_rsa.pub 增加参数 -f指定生产的名称,并且再提示中都直接回车,也就是不用输入密码
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_shs
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_gd
查看生成的秘钥
cat ~/.ssh/id_rsa_sh.pub
cat ~/.ssh/id_rsa_gd.pub
6
1
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_shs
2
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_gd
3
4
查看生成的秘钥
5
cat ~/.ssh/id_rsa_sh.pub
6
cat ~/.ssh/id_rsa_gd.pub
步骤二 配置~/.ssh/config文件
vi ~/.ssh/config
- guangdong Git
Host gdgit
HostName 172.26.6.67
User [email protected]
IdentityFile ~/.ssh/id_rsa_gd
- shanghai Git
Host shgit
HostName 116.228.151.221
User [email protected]
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_sh
Port 20022
Host就是每个SSH连接的单独代号,IdentityFile告诉SSH连接去读取哪个私钥。
检查配置
cat ~/.ssh/config
如有问题,可以用下面命令替换内容
cat >~/.ssh/config
- guangdong Git
Host gdgit
HostName 172.26.6.67
User [email protected]
IdentityFile ~/.ssh/id_rsa_gd
- shanghai Git
Host shgit
HostName 116.228.151.221
User [email protected]
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_sh
Port 20022
34
1
vi ~/.ssh/config
2
3
- guangdong Git
4
Host gdgit
5
HostName 172.26.6.67
6
User [email protected]
7
IdentityFile ~/.ssh/id_rsa_gd
8
9
- shanghai Git
10
Host shgit
11
HostName 116.228.151.221
12
User [email protected]
13
PreferredAuthentications publickey
14
IdentityFile ~/.ssh/id_rsa_sh
15
Port 20022
16
17
Host就是每个SSH连接的单独代号,IdentityFile告诉SSH连接去读取哪个私钥。
18
检查配置
19
cat ~/.ssh/config
20
如有问题,可以用下面命令替换内容
21
cat >~/.ssh/config
22
- guangdong Git
23
Host gdgit
24
HostName 172.26.6.67
25
User [email protected]
26
IdentityFile ~/.ssh/id_rsa_gd
27
28
- shanghai Git
29
Host shgit
30
HostName 116.228.151.221
31
User [email protected]
32
PreferredAuthentications publickey
33
IdentityFile ~/.ssh/id_rsa_sh
34
Port 20022
步骤三:执行ssh-agent让ssh识别新的私钥。
ssh-agent bash
ssh-add ~/.ssh/id_rsa_sh
ssh-add ~/.ssh/id_rsa_gd
参考如下:
$ ssh-agent bash
bash-3.1$ ssh-add ~/.ssh/id_rsa_sh
Identity added: /c/Users/xjch/.ssh/id_rsa_sh (/c/Users/xjch/.ssh/id_rsa_sh)
bash-3.1$ exit
exit
11
1
ssh-agent bash
2
ssh-add ~/.ssh/id_rsa_sh
3
ssh-add ~/.ssh/id_rsa_gd
4
5
参考如下:
6
$ ssh-agent bash
7
bash-3.1$ ssh-add ~/.ssh/id_rsa_sh
8
Identity added: /c/Users/xjch/.ssh/id_rsa_sh (/c/Users/xjch/.ssh/id_rsa_sh)
9
10
bash-3.1$ exit
11
exit
步骤四:打开git网站,进入账户控制面板,key,增加一个key,其中key信息就从第一步获取。
查看生成的秘钥
cat ~/.ssh/id_rsa_sh.pub
cat ~/.ssh/id_rsa_gd.pub
3
1
查看生成的秘钥
2
cat ~/.ssh/id_rsa_sh.pub
3
cat ~/.ssh/id_rsa_gd.pub
步骤五:验证是否成功
ssh -T [email protected]
Welcome to GitLab, ###!
ssh -T [email protected]
Welcome to GitLab, ###!
如果是第一次,可能需要提示你输入yes才可以
ssh -T [email protected]
The authenticity of host ‘[116.228.151.221]:20022 ([116.228.151.221]:20022)‘ can‘t be established.
RSA key fingerprint is 78:73:cb:9e:2a:83:4d:29:e3:de:62:c7:68:1c:46:2c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘[116.228.151.221]:20022‘ (RSA) to the list of known hosts.
Welcome to GitLab, ###!
12
1
ssh -T [email protected]
2
Welcome to GitLab, ###!
3
ssh -T [email protected]
4
Welcome to GitLab, ###!
5
如果是第一次,可能需要提示你输入yes才可以
6
7
ssh -T [email protected]
8
The authenticity of host ‘[116.228.151.221]:20022 ([116.228.151.221]:20022)‘ can‘t be established.
9
RSA key fingerprint is 78:73:cb:9e:2a:83:4d:29:e3:de:62:c7:68:1c:46:2c.
10
Are you sure you want to continue connecting (yes/no)? yes
11
Warning: Permanently added ‘[116.228.151.221]:20022‘ (RSA) to the list of known hosts.
12
Welcome to GitLab, ###!
第六步:拉取代码
广东git
$ git clone [email protected]:****/****.git
如果从git网站上获取的值为:[email protected]:****/****.git,需要将ip 172.26.6.67 替换为host:gdgit 如: [email protected]:****/****.git
上海git
$ git clone [email protected]:merchant/merchant_core.git
$ git clone [email protected]:merchant/merchant_portal.git
如果从git网站上获取的值为如:[email protected]:merchant/merchant_core.git,需要将localhost替换为host:shgit 如: [email protected]:merchant/merchant_core.git
参考文档:
http://www.2cto.com/os/201402/281792.html
http://my.oschina.net/csensix/blog/184434
~/.ssh/config的额外配置
TCPKeepAlive yes
IdentitiesOnly yes
TCPKeepAlive yes
当达成联机后,服务器会一直传送 TCP 封包给客户端藉以判断对方式否一直存在联机。
不过,如果联机时中间的路由器暂时停止服务几秒钟,也会让联机中断喔!
在这个情况下,任何一端死掉后,SSH可以立刻知道!而不会有僵尸程序的发生!
但如果你的网络或路由器常常不稳定,那么可以设定为 no 的啦!
IdentitiesOnly 只接受SSH key 登录
x
1
广东git
2
$ git clone [email protected]:****/****.git
3
4
如果从git网站上获取的值为:[email protected]:****/****.git,需要将ip 172.26.6.67 替换为host:gdgit 如: [email protected]:****/****.git
5
6
上海git
7
$ git clone [email protected]:merchant/merchant_core.git
8
$ git clone [email protected]:merchant/merchant_portal.git
9
10
如果从git网站上获取的值为如:[email protected]:merchant/merchant_core.git,需要将localhost替换为host:shgit 如: [email protected]:merchant/merchant_core.git
11
12
参考文档:
13
http://www.2cto.com/os/201402/281792.html
14
http://my.oschina.net/csensix/blog/184434
15
16
~/.ssh/config的额外配置
17
18
TCPKeepAlive yes
19
IdentitiesOnly yes
20
21
TCPKeepAlive yes
22
当达成联机后,服务器会一直传送 TCP 封包给客户端藉以判断对方式否一直存在联机。
23
不过,如果联机时中间的路由器暂时停止服务几秒钟,也会让联机中断喔!
24
在这个情况下,任何一端死掉后,SSH可以立刻知道!而不会有僵尸程序的发生!
25
但如果你的网络或路由器常常不稳定,那么可以设定为 no 的啦!
26
27
IdentitiesOnly 只接受SSH key 登录
以上是关于Git使用总结的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段15——git命令操作一个完整流程
VSCode自定义代码片段15——git命令操作一个完整流程
GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段