Skip to content

用户配置

概述

Git 安装完成后,第一件事就是配置用户信息。这些信息会记录在每一次提交中,用于标识提交者身份。正确的配置不仅有助于团队协作,还能确保提交历史的可追溯性。

Git 使用三级配置系统,允许在不同范围内设置不同的配置项。理解配置级别对于管理多个项目和团队协作非常重要。

配置用户名和邮箱

基本配置

bash
# 配置全局用户名
git config --global user.name "Your Name"

# 配置全局邮箱
git config --global user.email "your.email@example.com"

# 验证配置
git config --global user.name
# 输出: Your Name

git config --global user.email
# 输出: your.email@example.com

用户名命名规范

bash
# 推荐格式:真实姓名 或 GitHub 用户名

# 好的例子
git config --global user.name "Zhang San"
git config --global user.name "zhangsan"
git config --global user.name "Zhang San (Company)"

# 不推荐的例子
git config --global user.name "zhangsan123"
git config --global user.name "张三"

邮箱配置规范

bash
# 推荐使用与 Git 托管平台关联的邮箱

# GitHub 邮箱(推荐)
git config --global user.email "your-email@example.com"

# GitHub 提供的隐私邮箱
git config --global user.email "123456789+username@users.noreply.github.com"

# 公司邮箱(工作项目)
git config --global user.email "your.name@company.com"

# 验证邮箱格式
git config --global user.email

为特定仓库配置

bash
# 进入特定仓库
cd /path/to/repository

# 配置本地用户信息(仅对当前仓库有效)
git config --local user.name "Work Name"
git config --local user.email "work@company.com"

# 验证本地配置
git config --local user.name
git config --local user.email

配置级别

Git 使用三级配置系统,优先级从高到低:

配置级别详解

优先级:本地 > 全局 > 系统

┌─────────────────────────────────────────────────────────────┐
│                    Git 配置优先级                            │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  本地配置(--local)                                  │   │
│  │  文件位置:.git/config                               │   │
│  │  作用范围:当前仓库                                   │   │
│  │  优先级:最高                                         │   │
│  └─────────────────────────────────────────────────────┘   │
│                          ▼                                  │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  全局配置(--global)                                 │   │
│  │  文件位置:~/.gitconfig                              │   │
│  │  作用范围:当前用户所有仓库                           │   │
│  │  优先级:中等                                         │   │
│  └─────────────────────────────────────────────────────┘   │
│                          ▼                                  │
│  ┌─────────────────────────────────────────────────────┐   │
│  │  系统配置(--system)                                 │   │
│  │  文件位置:/etc/gitconfig                            │   │
│  │  作用范围:系统所有用户                               │   │
│  │  优先级:最低                                         │   │
│  └─────────────────────────────────────────────────────┘   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

本地配置(--local)

bash
# 查看本地配置文件
cat .git/config

# 设置本地配置
git config --local user.name "Project Name"
git config --local user.email "project@example.com"

# 查看本地配置
git config --local --list

# 删除本地配置
git config --local --unset user.name

全局配置(--global)

bash
# 查看全局配置文件
cat ~/.gitconfig

# 设置全局配置
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 查看全局配置
git config --global --list

# 删除全局配置
git config --global --unset user.name

系统配置(--system)

bash
# 查看系统配置文件
cat /etc/gitconfig

# 设置系统配置(需要管理员权限)
sudo git config --system user.name "System User"
sudo git config --system user.email "system@example.com"

# 查看系统配置
git config --system --list

# 删除系统配置
sudo git config --system --unset user.name

配置优先级示例

bash
# 场景:不同级别配置了不同的用户名

# 系统配置
sudo git config --system user.name "System User"

# 全局配置
git config --global user.name "Global User"

# 本地配置
git config --local user.name "Local User"

# 查看生效的配置
git config user.name
# 输出: Local User(本地配置优先级最高)

# 删除本地配置后
git config --local --unset user.name
git config user.name
# 输出: Global User(全局配置生效)

# 删除全局配置后
git config --global --unset user.name
git config user.name
# 输出: System User(系统配置生效)

查看配置

查看所有配置

bash
# 查看所有配置(合并显示)
git config --list

# 查看所有配置及来源
git config --list --show-origin

# 输出示例:
# file:/home/user/.gitconfig    user.name=Your Name
# file:/home/user/.gitconfig    user.email=your.email@example.com
# file:.git/config              user.name=Local User

查看特定级别配置

bash
# 查看系统配置
git config --system --list

# 查看全局配置
git config --global --list

# 查看本地配置
git config --local --list

查看单个配置项

bash
# 查看用户名
git config user.name

# 查看邮箱
git config user.email

# 查看编辑器
git config core.editor

# 查看默认分支名
git config init.defaultBranch

# 查看配置来源
git config --show-origin user.name
# 输出: file:/home/user/.gitconfig   Your Name

使用正则表达式查看

bash
# 查看所有 user 相关配置
git config --get-regexp user

# 查看所有 core 相关配置
git config --get-regexp core

# 查看所有 remote 相关配置
git config --get-regexp remote

常用配置项

用户信息配置

bash
# 用户名
git config --global user.name "Your Name"

# 邮箱
git config --global user.email "your.email@example.com"

# 默认分支名称
git config --global init.defaultBranch main

# 签名密钥(用于签名提交)
git config --global user.signingkey ABC12345

# 自动签名提交
git config --global commit.gpgsign true

编辑器配置

bash
# VS Code
git config --global core.editor "code --wait"

# Vim
git config --global core.editor "vim"

# Nano
git config --global core.editor "nano"

# Emacs
git config --global core.editor "emacs"

# Sublime Text
git config --global core.editor "subl -n -w"

# JetBrains IDE(如 PhpStorm)
git config --global core.editor "phpstorm --wait"

差异比较工具配置

bash
# 使用内置 diff
git config --global diff.tool vimdiff

# 使用 VS Code
git config --global diff.tool vscode
git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"

# 使用 Beyond Compare
git config --global diff.tool bc3

# 使用 meld
git config --global diff.tool meld

# 启动差异工具
git difftool

合并工具配置

bash
# 使用内置 merge
git config --global merge.tool vimdiff

# 使用 VS Code
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait $MERGED"

# 使用 Beyond Compare
git config --global merge.tool bc3

# 使用 meld
git config --global merge.tool meld

颜色配置

bash
# 启用颜色输出
git config --global color.ui auto

# 配置特定命令颜色
git config --global color.branch auto
git config --global color.diff auto
git config --global color.status auto

# 自定义颜色
git config --global color.diff.new "green bold"
git config --global color.diff.old "red bold"
git config --global color.diff.meta "yellow"

换行符配置

bash
# Windows
git config --global core.autocrlf true

# Linux/macOS
git config --global core.autocrlf input

# 禁用自动转换
git config --global core.autocrlf false

# 配置 safecrlf(检查换行符转换)
git config --global core.safecrlf warn

文件系统配置

bash
# 配置文件名大小写敏感
# Windows/macOS(默认不敏感)
git config --global core.ignorecase false

# 配置符号链接支持
git config --global core.symlinks true

# 配置文件权限
git config --global core.fileMode true

# 配置预提交钩子
git config --global core.hooksPath ~/.git/hooks

凭据管理配置

bash
# 缓存凭据(默认 15 分钟)
git config --global credential.helper cache

# 缓存凭据(自定义时间,如 8 小时)
git config --global credential.helper 'cache --timeout=28800'

# macOS:使用 Keychain
git config --global credential.helper osxkeychain

# Windows:使用 Git Credential Manager
git config --global credential.helper manager

# Linux:使用 libsecret
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

HTTP/HTTPS 配置

bash
# 配置代理
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy https://proxy.example.com:8080

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

# 配置 SSL 验证
git config --global http.sslVerify true

# 配置超时
git config --global http.lowSpeedLimit 1000
git config --global http.lowSpeedTime 5

# 配置 POST 缓冲区大小
git config --global http.postBuffer 524288000

日志配置

bash
# 配置默认日志格式
git config --global format.pretty oneline

# 配置签名格式
git config --global format.signoff true

# 配置 reflog 过期时间
git config --global gc.reflogExpire 90.days.ago
git config --global gc.reflogExpireUnreachable 30.days.ago

性能优化配置

bash
# 启用文件系统监控(提高大仓库性能)
git config --global core.fsmonitor true

# 启用未跟踪文件缓存
git config --global core.untrackedCache true

# 配置打包参数
git config --global pack.threads 8
git config --global pack.deltaCacheSize 2047m
git config --global pack.windowMemory 2047m

配置文件详解

全局配置文件结构

ini
# ~/.gitconfig

[user]
    name = Your Name
    email = your.email@example.com
    signingkey = ABC12345

[core]
    editor = code --wait
    autocrlf = input
    ignorecase = false
    quotepath = false

[init]
    defaultBranch = main

[color]
    ui = auto

[alias]
    st = status
    co = checkout
    br = branch
    ci = commit
    lg = log --oneline --graph --all

[credential]
    helper = osxkeychain

[diff]
    tool = vscode

[merge]
    tool = vscode

[push]
    default = simple
    autoSetupRemote = true

[pull]
    rebase = false

本地配置文件结构

ini
# .git/config

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true

[user]
    name = Project User
    email = project@company.com

[remote "origin"]
    url = https://github.com/user/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*

[branch "main"]
    remote = origin
    merge = refs/heads/main

直接编辑配置文件

bash
# 编辑全局配置
git config --global --edit
# 或
vim ~/.gitconfig

# 编辑本地配置
git config --local --edit
# 或
vim .git/config

# 编辑系统配置
sudo git config --system --edit
# 或
sudo vim /etc/gitconfig

多账号配置

场景:同时使用 GitHub 和 GitLab

bash
# 方法 1:使用条件配置(Git 2.13+)

# 编辑全局配置
vim ~/.gitconfig

# 添加条件配置
[includeIf "gitdir:~/work/"]
    path = ~/.gitconfig-work

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

# 创建工作配置
vim ~/.gitconfig-work

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

# 创建个人配置
vim ~/.gitconfig-personal

[user]
    name = Personal Name
    email = personal@example.com

使用 SSH 配置多账号

bash
# 编辑 SSH 配置
vim ~/.ssh/config

# GitHub 个人账号
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_personal

# GitHub 工作账号
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_work

# GitLab 账号
Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_rsa_gitlab

# 克隆仓库时使用
git clone git@github-work:company/repo.git

使用脚本切换配置

bash
# 创建切换脚本
cat > ~/git-switch.sh << 'EOF'
#!/bin/bash

if [ "$1" = "work" ]; then
    git config --global user.name "Work Name"
    git config --global user.email "work@company.com"
    echo "Switched to work account"
elif [ "$1" = "personal" ]; then
    git config --global user.name "Personal Name"
    git config --global user.email "personal@example.com"
    echo "Switched to personal account"
else
    echo "Usage: git-switch.sh [work|personal]"
fi
EOF

chmod +x ~/git-switch.sh

# 使用
~/git-switch.sh work
~/git-switch.sh personal

配置备份与恢复

备份配置

bash
# 导出全局配置
git config --global --list > ~/gitconfig-backup.txt

# 复制配置文件
cp ~/.gitconfig ~/.gitconfig.backup

# 备份 SSH 配置
cp -r ~/.ssh ~/.ssh.backup

恢复配置

bash
# 从备份恢复
cp ~/.gitconfig.backup ~/.gitconfig

# 从导出文件恢复
while read line; do
    key=$(echo $line | cut -d= -f1)
    value=$(echo $line | cut -d= -f2-)
    git config --global "$key" "$value"
done < ~/gitconfig-backup.txt

同步配置到新机器

bash
# 在新机器上快速配置
# 方法 1:复制配置文件
scp old-machine:~/.gitconfig ~/.gitconfig

# 方法 2:使用脚本
cat > ~/setup-git.sh << 'EOF'
#!/bin/bash
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main
git config --global color.ui auto
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all"
EOF

chmod +x ~/setup-git.sh
~/setup-git.sh

总结

必做配置清单

bash
# 1. 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# 2. 配置默认分支名
git config --global init.defaultBranch main

# 3. 配置编辑器
git config --global core.editor "code --wait"

# 4. 配置换行符
git config --global core.autocrlf input  # Linux/macOS
# 或
git config --global core.autocrlf true   # Windows

# 5. 配置凭据管理
git config --global credential.helper osxkeychain  # macOS
# 或
git config --global credential.helper manager      # Windows

# 6. 验证配置
git config --global --list

配置最佳实践

  1. 使用全局配置作为默认:大部分项目使用相同的用户信息
  2. 本地配置覆盖特殊情况:工作项目使用公司邮箱
  3. 定期备份配置文件:避免重装系统后丢失配置
  4. 使用条件配置管理多账号:Git 2.13+ 的 includeIf 功能
  5. 配置有意义的别名:提高工作效率

下一步

配置完成后,建议继续学习: