Git 笔记

Git 配置 SSH 免密登录

设置用户名和邮箱

1
2
3
4
5
6
7
8
#设置用户名
git config --global user.name "name"
#查看当前用户名
git config --global user.name
#设置邮箱
git config --global user.email "user@emial.com"
#查看当前邮箱
git config --global user.email

生成密钥

1
2
3
4
5
6
7
8
#首先进入用户的主目录中
cd ~
#配置免密登录命令
ssh-keygen -t rsa -C "zhangsan@qq.com"
#按三次回车,免密登录文件会保存到电脑的 /c/Users/{Username}/.ssh 目录下
#输入cat命令查看秘钥
cat ~/.ssh/id_rsa.pub
#复制秘钥内容到git添加自己的公钥

远程仓库配置密钥

登录 github,打开 setting 配置,在设置界面选择 SSH and GPG keys,选择 new SSH key,输入 key。

Git 设置代理

1、全局代理:

1
2
3
4
5
6
7
# SOCKS5 协议,1080 端口修改成自己的本地 SOCKS5 代理端口
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080

# HTTP 协议,1081 端口修改成自己的本地 HTTP 代理端口
git config --global http.proxy http://127.0.0.1:1081
git config --global https.proxy https://127.0.0.1:1081

2、只代理 github:

1
2
3
4
5
6
7
8
# SOCKS5 协议,1080 端口修改成自己的本地 SOCKS5 代理端口
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080

git config --global https.https://github.com.proxy socks5://127.0.0.1:1080

# HTTP 协议,1081 端口修改成自己的本地 HTTP 代理端口
git config --global http.https://github.com.proxy https://127.0.0.1:1081
git config --global https.https://github.com.proxy https://127.0.0.1:1081

3、取消 / 重置代理:

1
2
git config --global --unset http.proxy
git config --global --unset https.proxy

4、设置 ssh 代理
设置 ssh 代理前,请确保你已经设置 ssh key。更进一步是设置 ssh 代理。只需要配置一个 config 就可以了。

1
2
3
4
# Linux、MacOS
vi ~/.ssh/config
# Windows
到C:\Users\your_user_name\.ssh目录下,新建一个config文件(无后缀名)

将下面内容加到 config 文件中即可对于 windows 用户,代理会用到 connect.exe,你如果安装了 Git 都会自带 connect.exe,路径为 C:\APP\Git\mingw64\bin\connect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#Windows用户,注意替换你的端口号和connect.exe的路径
ProxyCommand "C:\APP\Git\mingw64\bin\connect" -S 127.0.0.1:51837 -a none %h %p

#MacOS用户用下方这条命令,注意替换你的端口号
#ProxyCommand nc -v -x 127.0.0.1:51837 %h %p

Host github.com
User git
Port 22
Hostname github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\Your_User_Name\.ssh\id_rsa"
TCPKeepAlive yes

Host ssh.github.com
User git
Port 443
Hostname ssh.github.com
# 注意修改路径为你的路径
IdentityFile "C:\Users\Your_User_Name\.ssh\id_rsa"
TCPKeepAlive yes