git 基本配置

文章目錄

  • git 基本配置
  • 1. ssh 生成公鑰
  • 2. git 配置查詢
  • 3. git 初始用户設置
  • 4. 代理設置
  • 參考

1. ssh 生成公鑰

Github 等代碼託管平台都可以通過設置ssh公鑰從而免去輸入賬號和密碼來同步代碼倉。因此需要首先生成本地的ssh 公鑰。操作如下。

$ ssh-keygen -t rsa -C "your email"

接下來如果之前沒有設置過就直接三次回車就好,如果之前設置過,則會提示是否要覆蓋,敲Y即可。出現下面的頁面就代表你已經設置成功了。 -C選項及後面的email可以不寫。
使用下面的這條指令可以查看生成的秘鑰,複製到github的設置中,就可使用了。

$ cat ~/.ssh/id_rsa.pub

2. git 配置查詢

$ git config --list

3. git 初始用户設置

安裝git後,第一次提交代碼的時候會要求設置用户名和email,設置如下:

$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

設置默認編輯器:

$ git config --global core.editor vim  # 可以選擇自己的編輯器,如 emacs、vs code。

4. 代理設置

國內直接訪問github速度比較慢,如果有代理服務器的話可以設置git代理。

Git 目前支持的三種協議 git://、ssh:// 和 http://,其代理配置各不相同:core.gitproxy 用於 git:// 協議,http.proxy 用於 http:// 協議,ssh:// 協議的代理需要配置 ssh 的 ProxyCommand 參數。

a. 設置代理

git config --global http.proxy 'socks5://127.0.0.1:1080'      # 設置http代理
git config --global https.proxy 'socks5://127.0.0.1:1080'     # 設置https代理
git config --global core.gitproxy 'socks5://127.0.0.1:1080'   # 設置git代理

b. 查看代理

git config --global --get http.proxy
git config --global --get https.proxy
git config --global --get core.gitproxy

c. 取消代理

git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset core.gitproxy

d. 使用 ssh 協議的 git 配置代理

# linux 系統在~/.ssh/config 文件後面添加幾行,沒有可以新建一個
Host github.com
User git
# SSH默認端口22, HTTPS默認端口443
Port 22
Hostname %h
# 這裏放你的SSH私鑰
IdentityFile ~\.ssh\id_rsa # 這裏寫自定義目錄標題
# 設置代理, 127.0.0.1:1080 換成你自己代理軟件監聽的本地地址
# HTTPS使用-H,SOCKS使用-S
ProxyCommand connect -S 127.0.0.1:1080 %h %p
# windows 系統在C:\Users\用户名\.ssh\config,不存在自行創建
ProxyCommand connect -S 127.0.0.1:1080 -a none %h %p 
Host github.com
User git
Port 22
Hostname github.com
# 注意修改路徑為你的路徑 
IdentityFile "C:\Users\用户名\.ssh\id_rsa" 
TCPKeepAlive yes

參考

1.6 起步 - 初次運行 Git 前的配置

8.1 自定義 Git - 配置 Git

Git設置代理和取消代理的方式

如何為 Git 設置代理?

Git設置代理

git使用代理服務器的設置方法

[整理]為 git 和 ssh 設置 socks5 協議的代理

如何為Git設置代理