初始化配置
配置用戶信息
安裝完 Git 之後,要做的第一件事就是設置你的用戶名和郵件地址。這一點很重要,因爲每一個 Git 提交都會使用這些信息,它們會寫入要你的每一次提交中,不可更改:
git config --global user.name "用戶名"
git config --global user.email "郵箱"
–global 選項,這會對你係統上所有的倉庫生效!
配置代理
配置http代理
# 配置 http 代理
git config --global http.proxy "http://127.0.0.1:1080"
git config --global https.proxy "http://127.0.0.1:1080"
# 配置 socks5 代理
git config --global http.proxy "socks5://127.0.0.1:1080"
git config --global https.proxy "socks5://127.0.0.1:1080"
以上的配置會導致所有 git 命令都走代理,但是如果你混合使用了國內的 git 倉庫,下面是僅僅針對 Github 進行配置,其它的保持不變:
# 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
# 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
配置ssh代理
ssh配置文件地址爲:~/.ssh/config
windows中就是:C:\Users\你的用戶名\.ssh\config
(若不存在自行創建)
配置文件中增加以下內容:
Host github.com *.github.com
User git
# SSH默認端口22, HTTPS默認端口443
Port 22
Hostname %h
# 這裏放你的SSH私鑰
IdentityFile ~\.ssh\id_rsa
# 設置代理, 127.0.0.1:10808 換成你自己代理軟件監聽的本地地址
# HTTPS使用-H,SOCKS使用-S
ProxyCommand connect -S 127.0.0.1:10808 %h %p
查看配置信息
git config --list
ssh配置
使用 SSH 協議可以連接遠程服務器和服務並向它們驗證。利用 SSH 密鑰可以連接 GitHub,而無需在每次訪問時輸入用戶名和密碼
生成SSH密鑰
- 打開 Git Bash
- 輸入如下命令生成密鑰
ssh-keygen -t rsa -b 4096 -C "[email protected]"
這將創建以所提供的電子郵件地址爲標籤的新 SSH 密鑰
添加密鑰到 ssh-agent
- 啓動 ssh-agent
# 在後臺啓動 ssh-agent
eval $(ssh-agent -s)
- 將 SSH 私鑰添加到 ssh-agent,如果您創建了不同名稱的密鑰,或者您要添加不同名稱的現有密鑰,請將命令中的 id_rsa 替換爲您的私鑰文件的名稱。
ssh-add ~/.ssh/id_rsa
GitHub 添加公鑰
要配置 GitHub 帳戶使用新的(或現有)SSH 密鑰,您還需要將其添加到 GitHub 帳戶