希曼日记

基础篇:Git 配置

约 1 分钟395 字

在配置用户邮箱时,推荐使用在 GitHub/GitLab 绑定并验证过的真实邮箱,这样提交才会计入你的账号、显示在项目仓库的作者里:

GitHub 仓库侧边栏的 Contributors 里显示出提交者账号

示例配置:

Shell
git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com

推荐的~/.gitconfig设置:

INI
[color]
ui = auto
[core]
pager = more
autocrlf = false
[http]
proxy = http://127.0.0.1:7890
[https]
proxy = http://127.0.0.1:7890
[branch]
sort = -committerdate

详细介绍:

Text
[color]
ui = auto
  • 启用 Git 命令行彩色输出。
  • 影响 git status、git diff、git log 等命令。
Text
[core]
pager = less
autocrlf = false
  • pager = more:长输出通过 more 分页显示,例如 git log 和 git diff。
  • pager = less:支持上下滚动、搜索和退出:
Text
[core]
pager = less
  • autocrlf = false:Git 不自动转换换行符。
  • 检出和提交时保留文件原有的 LF 或 CRLF。
  • macOS/Linux 通常适合设为 false。
  • Windows 是否适合取决于项目的 .gitattributes;现代跨平台项目通常由 .gitattributes 统一控制换行。
Text
[http]
proxy = http://127.0.0.1:7890
[https]
proxy = http://127.0.0.1:7890
  • Git 访问 HTTP/HTTPS 远程仓库时,通过本机 7890 端口的代理。
  • 常用于 Clash 等本地代理程序。
  • 即使访问 HTTPS 地址,代理协议也经常写成 http://,这是正常的 HTTP CONNECT 代理形式。
  • 本地代理未运行时,git clone、git pull、git push 可能出现连接失败。
  • 这只影响 Git 的 HTTP/HTTPS 网络请求,不影响 SSH 地址,例如:
Text
git@github.com:owner/repository.git

通常只设置 [http] proxy 就足够覆盖 HTTPS 请求。也可以仅针对 GitHub 配置,避免影响其他站点:

Text
[http "https://github.com"]
proxy = http://127.0.0.1:7890
Text
[branch]
sort = -committerdate
  • 控制 git branch 的默认排序方式。
  • committerdate 表示按最近一次提交的时间排序。
  • 前面的 - 表示降序,即最近更新的分支显示在最前面。

可以查看配置来自哪个文件:

Text
git config --list --show-origin

分别查看全局和当前仓库配置:

Text
git config --global --list
git config --local --list

具体配置可以参考Git 基础配置。