Skip to content

附录 B:.gitconfig 推荐配置

完整的 ~/.gitconfig 配置参考,根据需要按需选用。

完整配置示例

ini
[user]
    name = Your Name
    email = you@example.com
    # 如果使用 GPG 签名提交,设置 signingkey
    # signingkey = ABCD1234

[core]
    # 默认编辑器
    editor = vim
    # 使用 delta 作为 pager(需安装 git-delta)
    pager = delta
    # 自动将 CRLF 转换为 LF(macOS/Linux)
    autocrlf = input
    # Windows 用户使用 true(checkout 时转 CRLF,commit 时转 LF)
    # autocrlf = true
    # 忽略文件名大小写(macOS/Windows 建议设 false)
    ignorecase = false
    # 全局 .gitignore 文件路径
    excludesfile = ~/.gitignore_global
    # 处理超长路径(Windows 需要)
    longpaths = true

[init]
    # 新仓库的默认分支名
    defaultBranch = main

[pull]
    # 拉取时使用 rebase(保持线性历史)
    rebase = true
    # 如果不想用 rebase,注释上面一行,用下面的:
    # ff = only

[push]
    # 只推送当前分支到同名远程分支
    default = current
    # 自动推送 tags
    # followTags = true

[fetch]
    # 自动清理已删除的远程分支的追踪引用
    prune = true

[merge]
    # 合并时显示原始版本(三路合并)
    conflictstyle = diff3
    # 使用 vimdiff 作为合并工具
    tool = vimdiff

[rebase]
    # 交互式 rebase 时自动 stash
    autostash = true
    # 自动将 fixup! 提交移到正确位置
    autosquash = true

[diff]
    # 使用 delta 工具(需安装)
    # tool = vscode
    # 使用 VS Code 作为 diff 工具
    # [difftool "vscode"]
    #     cmd = code --wait --diff $LOCAL $REMOTE

[interactive]
    # 使用 delta 处理交互式 diff(需安装 delta)
    diffFilter = delta --color-only

[delta]
    # delta 配置(需安装 git-delta)
    navigate = true
    light = false
    side-by-side = true
    line-numbers = true
    syntax-theme = Dracula

[color]
    # 启用颜色输出
    ui = auto
    branch = auto
    diff = auto
    status = auto

[color "status"]
    added = green bold
    changed = yellow bold
    untracked = red bold

[credential]
    # 凭据缓存(macOS)
    helper = osxkeychain
    # Linux 使用:
    # helper = store
    # Windows 使用:
    # helper = manager

[http]
    # 设置代理(如果需要)
    # proxy = http://127.0.0.1:7890
    # SSL 验证(通常保持默认 true,除非确实有需要)
    # sslVerify = false

[rerere]
    # 自动记录并重用冲突解决方案
    enabled = true

[alias]
    # ===== 状态 & 基础 =====
    st = status
    s = status -s
    
    # ===== 添加 =====
    aa = add .
    ap = add -p
    
    # ===== 提交 =====
    cm = commit
    ca = commit --amend
    can = commit --amend --no-edit
    
    # ===== 切换 & 分支 =====
    co = checkout
    sw = switch
    br = branch
    bra = branch -a
    brv = branch -v
    
    # ===== 日志 =====
    lg = log --oneline --graph --all --decorate
    ll = log --oneline -15
    last = log -1 HEAD
    lds = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Creset\\ %s\\ %C(red)[%an]" --date=short
    
    # ===== 差异 =====
    df = diff
    dfc = diff --cached
    dfh = diff HEAD
    dfs = diff --stat
    
    # ===== 推送 & 拉取 =====
    psh = push
    pshf = push --force-with-lease
    pl = pull
    plr = pull --rebase
    fet = fetch
    fetp = fetch --prune
    
    # ===== 撤销 =====
    unstage = restore --staged
    undo = reset --soft HEAD~1
    discard = restore
    
    # ===== 储藏 =====
    ss = stash
    ssl = stash list
    ssp = stash pop
    ssa = stash apply
    
    # ===== 远程 =====
    rv = remote -v
    
    # ===== 实用工具 =====
    aliases = config --get-regexp alias
    who = shortlog -sn
    find = log --all --full-history --
    cleanup = !git branch --merged | grep -v '\\*\\|main\\|master\\|develop' | xargs -n 1 git branch -d

[gpg]
    # GPG 签名(可选)
    # program = gpg

[commit]
    # 自动 GPG 签名所有提交(可选)
    # gpgsign = true
    # 显示提交差异(commit 时在编辑器中显示 diff)
    verbose = true

分平台配置

有时候在不同操作系统上需要不同配置:

ini
# 使用条件包含,根据目录自动切换配置
[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

[includeIf "gitdir:~/personal/"]
    path = ~/.gitconfig-personal

~/.gitconfig-work

ini
[user]
    name = Your Work Name
    email = work@company.com

~/.gitconfig-personal

ini
[user]
    name = Your Personal Name
    email = personal@gmail.com

.gitignore_global 推荐内容

~/.gitignore_global 中添加操作系统和编辑器生成的文件:

# macOS
.DS_Store
.AppleDouble
.LSOverride
._*

# Windows
Thumbs.db
ehthumbs.db
Desktop.ini

# Linux
*~

# VS Code
.vscode/
*.code-workspace

# JetBrains IDEs
.idea/
*.iml
*.ipr
*.iws

# Vim
*.swp
*.swo
*~

# Emacs
*#
.#*

# Node.js
node_modules/
npm-debug.log*

# Python
__pycache__/
*.pyc
*.pyo
.env

# Java
*.class
*.jar
target/

# 操作系统临时文件
*.tmp
*.bak
*.orig
*.log

常见问题排查

bash
# 查看最终生效的配置(合并 system + global + local)
git config --list --show-origin

# 查看某个配置的来源
git config --show-origin user.email

# 重置某个配置
git config --global --unset core.autocrlf

# 清除过时的全局配置
git config --global --edit