git config 配置

git config 是 Git 核心配置工具,用于设置/查看 Git 全局、仓库级或系统级的配置参数,决定 Git 的行为方式。以下是核心用法高频常用配置,覆盖日常开发场景。

一、核心基础

1. 配置级别

优先级:local > global > system。

级别 参数 作用范围 配置文件路径
仓库级(默认) –local 仅当前 Git 仓库生效 仓库根目录 .git/config
用户级 –global 当前用户所有仓库生效 Linux/Mac:~/.gitconfig;Windows:C:\Users\<用户名>\.gitconfig
系统级 –system 系统所有用户所有仓库生效 Linux/Mac:/etc/gitconfig;Windows:Git安装目录/etc/gitconfig

2. 基础操作命令

(1)查看配置

# 查看所有配置(按优先级合并)
git config --list

# 查看指定级别配置
git config --global --list  # 仅全局配置
git config --local --list   # 仅当前仓库配置

# 查看单个配置项(如用户名)
git config user.name

(2)设置配置

# 格式:git config [--global/--local/--system] <key> <value>
git config --global user.name "Your Name"  # 全局设置用户名

(3)删除配置

git config --global --unset user.email  # 删除全局邮箱配置
git config --local --unset core.editor  # 删除当前仓库编辑器配置

二、高频常用配置

1. 基础身份配置(必须)

Git 提交时必须关联用户名和邮箱,否则无法提交,优先全局配置

# 全局设置用户名(建议和 Git 平台(GitHub/Gitee)一致)
git config --global user.name "Zhang San"
# 全局设置邮箱(建议和 Git 平台绑定的邮箱一致)
git config --global user.email "zhangsan@example.com"

# (可选)仅当前仓库覆盖全局身份(如多账号场景)
git config --local user.email "zhangsan_company@example.com"

2. 编辑器配置

解决默认编辑器(如 vim)操作不便问题,指定 Git 执行 commitrebase 等操作时的默认编辑器:

# 配置 VS Code 为默认编辑器(需先将 code 加入系统环境变量)
git config --global core.editor "code --wait"

# 配置 Notepad++(Windows)
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

# 配置 Vim(Linux/Mac 常用)
git config --global core.editor "vim"

# 配置 sublime/Typora
# Typora 需注意:添加 --wait(等待关闭)+ --new-window(新开窗口,避免复用已有窗口导致 Git 识别异常)。
# 方法1:直接指定完整路径(推荐,避免环境变量问题)
git config --global core.editor "'C:/Program Files/Sublime Text/sublime_text.exe' --wait"

# 方法2:若已配置环境变量,简化为
git config --global core.editor "sublime_text --wait"

3. 换行符配置(跨平台兼容关键)

Windows/Linux/Mac 换行符(CRLF/LF)不一致会导致文件格式问题,必配

# Windows:拉取时自动将 LF 转为 CRLF,提交时转回 LF
git config --global core.autocrlf true

# Linux/Mac:拉取时不转换,提交时强制转为 LF
git config --global core.autocrlf input

# (可选)检查换行符问题(提交时若混合换行符则警告)
git config --global core.safecrlf warn

4. 别名配置

将常用的长命令缩写为短别名,是提升 Git 效率的核心配置:

# 基础别名(推荐全局配置)
git config --global alias.st status          # git st → git status
git config --global alias.ci commit          # git ci → git commit
git config --global alias.co checkout        # git co → git checkout
git config --global alias.br branch          # git br → git branch
git config --global alias.unstage "reset HEAD --"  # git unstage <文件> → 撤销暂存
git config --global alias.last "log -1 HEAD"       # git last → 查看最后一次提交

# 高级别名:美化提交日志(git lg 一键查看清晰日志)
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

使用示例:

git st          # 等价于 git status
git lg          # 查看带分支图、颜色、作者的简洁日志
git last        # 查看最后一次提交详情

5. 颜色配置

全局开启 Git 命令颜色高亮。

# 自动为所有 Git 命令输出启用颜色(推荐 auto,仅终端输出时显示颜色)
git config --global color.ui auto

# (可选)单独配置某类命令的颜色(如日志)
git config --global color.log.diff "red bold"

6. 免密/凭证缓存

缓存 Git 仓库的账号密码,无需每次拉取/推送都输入:

# Windows:使用系统凭据管理器缓存
git config --global credential.helper wincred

# Mac:使用系统钥匙串缓存(永久有效)
git config --global credential.helper osxkeychain

# Linux:缓存 1 小时(3600 秒),超时后需重新输入
git config --global credential.helper "cache --timeout=3600"

注:若使用 SSH 密钥连接仓库(如 GitHub/Gitee),无需此配置。

7. 拉取/推送配置

优化合并/推送行为。

# 拉取时用 rebase 代替 merge(避免多余的 merge 提交,保持提交记录整洁)
git config --global pull.rebase true

# 推送时仅推送当前分支(现代 Git 推荐,避免误推其他分支)
git config --global push.default simple

三、进阶配置

1. 日志日期格式

统一日志时间显示。

# 日志日期显示为 ISO 8601 格式(如 2025-12-15T10:00:00+08:00)
git config --global log.date iso8601

# 或显示为相对时间(如 "2 hours ago")
git config --global log.date relative

2. 合并工具配置

解决冲突更友好。

# 配置 meld 为合并工具(Linux/Mac,需先安装 meld)
git config --global merge.tool meld
git config --global mergetool.meld.path /usr/bin/meld

# 配置 Beyond Compare(Windows)
git config --global merge.tool bc3
git config --global mergetool.bc3.path "C:/Program Files/Beyond Compare 4/bcomp.exe"

3. 分页工具配置

优化命令输出分页。

# 指定 less 为分页工具,支持颜色和换行
git config --global core.pager "less -r"

# 禁用分页(如 git status 不再分页)
git config --global core.pager ""

四、配置文件直接编辑

除了命令行,也可直接编辑配置文件(更直观):

  • 全局配置:vim ~/.gitconfig(Linux/Mac)或直接打开 C:\Users\<用户名>\.gitconfig(Windows)
  • 仓库配置:vim .git/config(当前仓库根目录)

示例 .gitconfig 完整内容(参考):

[user]
    name = Zhang San
    email = zhangsan@example.com
[core]
    editor = code --wait
    autocrlf = true
    safecrlf = warn
[alias]
    st = status
    ci = commit
    co = checkout
    br = branch
    lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
[color]
    ui = auto
[credential]
    helper = wincred
[pull]
    rebase = true
[push]
    default = simple