初始化配置
配置用户信息
安装完 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 config --global --unset http.proxy
git config --global --unset https.proxy
以上的配置会导致所有 git 命令都走代理,但是如果你混合使用了国内的 git 仓库,下面是仅仅针对 Github 进行配置,其它的保持不变:
# http协议
git config --global http.https://github.com.proxy http://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 "your_email@example.com"
这将创建以所提供的电子邮件地址为标签的新 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 帐户