跳转到内容

Git 设置用户信息 配置 Git 用户名 和 邮箱

Git User Configuration

正确配置 Git 用户信息是版本控制的基础。每次提交都会记录作者信息,因此确保配置正确非常重要。本文将详细介绍 Git 用户配置的各个方面。

查看git配置信息

sh
# 查看所有配置
git config --list

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

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

# 查看当前仓库配置
git config --local --list

# 查看特定配置项
git config user.name
git config user.email
git config core.editor

配置文件位置

Git 的配置存储在三个不同层级的文件中:

bash
# 系统级配置(所有用户)
/etc/gitconfig
# 或 C:\Program Files\Git\etc\gitconfig (Windows)

# 全局配置(当前用户)
~/.gitconfig
# 或 ~/.config/git/config
# 或 C:\Users\Username\.gitconfig (Windows)

# 本地配置(当前仓库)
/path/to/repo/.git/config

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

查看git用户名、密码、邮箱的配置

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

# 查看邮箱
git config user.email

# 注意:Git 不存储密码
# 密码由凭证管理器处理
git config credential.helper

检查配置是否正确

bash
# 创建一个测试提交来验证配置
git commit --allow-empty -m "Test commit"
git log -1 --format="Author: %an <%ae>"

# 应该输出类似:
# Author: Your Name <your.email@example.com>

设置git用户名、邮箱的配置

Git 提供了灵活的配置层级,你可以根据需要选择配置范围:

全局配置(推荐)

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

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

# 用户名:建议使用注册 GitHub 时用的用户名
# 邮箱:建议使用注册 GitHub 时用的邮箱

使用场景:

  • 个人电脑上的所有项目使用同一身份
  • 自由职业者或个人项目

项目级配置

bash
# 进入项目目录
cd /path/to/project

# 设置项目特定的用户名
git config user.name "Project Contributor"

# 设置项目特定的邮箱
git config user.email "project@example.com"

使用场景:

  • 工作电脑上有多个身份(工作/个人)
  • 开源项目贡献需要使用特定邮箱
  • 客户项目需要使用客户提供的邮箱

系统级配置

bash
# 需要管理员权限
sudo git config --system user.name "System User"
sudo git config --system user.email "system@example.com"

使用场景:

  • 服务器环境
  • 共享开发环境
  • CI/CD 系统

临时覆盖配置

bash
# 单次提交使用不同的身份
git -c user.name="Temporary Name" \
    -c user.email="temp@example.com" \
    commit -m "Temporary commit"

# 或在提交时指定
GIT_AUTHOR_NAME="Temp Name" \
GIT_AUTHOR_EMAIL="temp@example.com" \
GIT_COMMITTER_NAME="Temp Name" \
GIT_COMMITTER_EMAIL="temp@example.com" \
git commit -m "Temporary commit"

高级配置选项

配置签名邮箱

bash
# 设置签名邮箱(与提交邮箱可以不同)
git config --global user.signingkey YOUR_GPG_KEY_ID

# 启用自动签名
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"

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

# Atom
git config --global core.editor "atom --wait"

配置合并工具

bash
# 使用 meld
git config --global merge.tool meld
git config --global mergetool.prompt false

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

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

配置差异工具

bash
# 使用 meld
git config --global diff.tool meld
git config --global difftool.prompt false

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

使用 SSH 的方式

SSH 密钥提供了更安全、更便捷的 Git 认证方式,无需每次输入密码。

生成 SSH 密钥对

sh
# 基本生成命令
ssh-keygen -t rsa -C "your.email@example.com"

# 推荐的现代算法(更安全)
ssh-keygen -t ed25519 -C "your.email@example.com"

# 指定文件名和路径
ssh-keygen -t ed25519 -C "your.email@example.com" -f ~/.ssh/github_ed25519

# 添加密码保护(推荐)
ssh-keygen -t ed25519 -C "your.email@example.com" -N "your_passphrase"

生成过程中的选项:

bash
$ ssh-keygen -t ed25519 -C "your.email@example.com"

# 1. 选择保存路径(直接回车使用默认)
Enter file in which to save the key (/home/user/.ssh/id_ed25519):

# 2. 设置密码(可选,但推荐)
Enter passphrase (empty for no passphrase): [输入密码]
Enter same passphrase again: [确认密码]

密钥文件说明

公钥:id_ed25519.pub  (或 id_rsa.pub)
私钥:id_ed25519      (或 id_rsa)

⚠️ 重要:永远不要分享你的私钥!

查看公钥内容

bash
# 查看公钥
cat ~/.ssh/id_ed25519.pub

# 输出示例:
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your.email@example.com

添加 SSH 密钥到 GitHub/GitLab

GitHub

  1. 复制公钥内容

    bash
    cat ~/.ssh/id_ed25519.pub | pbcopy  # macOS
    cat ~/.ssh/id_ed25519.pub | clip    # Windows
    xclip -sel clip < ~/.ssh/id_ed25519.pub  # Linux
  2. 访问 https://github.com/settings/keys

  3. 点击 "New SSH key"

  4. 粘贴公钥并保存

GitLab

  1. 访问 https://gitlab.com/-/profile/keys

  2. 点击 "Add new key"

  3. 粘贴公钥并保存

配置 SSH Config

创建或编辑 ~/.ssh/config 文件:

bash
# 创建 config 文件
touch ~/.ssh/config
chmod 600 ~/.ssh/config

添加以下内容:

ssh-config
# GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

# GitLab
Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

# 公司内部 GitLab
Host git.company.com
  HostName git.company.com
  User git
  IdentityFile ~/.ssh/company_ed25519
  IdentitiesOnly yes

测试 SSH 连接

bash
# 测试 GitHub
ssh -T git@github.com
# 成功输出:Hi username! You've successfully authenticated...

# 测试 GitLab
ssh -T git@gitlab.com
# 成功输出:Welcome to GitLab, @username!

切换远程仓库为 SSH

bash
# 查看当前远程
git remote -v

# 从 HTTPS 切换到 SSH
git remote set-url origin git@github.com:username/repo.git

# 或者先删除再添加
git remote remove origin
git remote add origin git@github.com:username/repo.git

# 验证
git remote -v

多账户管理

在实际工作中,你可能需要管理多个 Git 账户(如工作账户和个人账户)。

方法一:使用条件包含

~/.gitconfig 中:

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

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

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

创建 ~/.gitconfig-work

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

创建 ~/.gitconfig-personal

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

方法二:使用不同的 SSH 密钥

~/.ssh/config 中:

ssh-config
# 工作账户
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/work_ed25519
  IdentitiesOnly yes

# 个人账户
Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/personal_ed25519
  IdentitiesOnly yes

克隆仓库时使用:

bash
# 工作项目
git clone git@github-work:company/project.git

# 个人项目
git clone git@github-personal:username/project.git

方法三:使用 git-credential-manager

bash
# 安装凭证管理器
brew install git-credential-manager  # macOS
sudo apt install git-credential-manager  # Linux

# 配置
git config --global credential.helper manager-core

# 存储不同账户的凭证
git credential-manager store

凭证管理

配置凭证助手

bash
# macOS
git config --global credential.helper osxkeychain

# Windows
git config --global credential.helper wincred

# Linux (GNOME Keyring)
git config --global credential.helper libsecret

# Linux (通用缓存)
git config --global credential.helper 'cache --timeout=3600'

手动管理凭证

bash
# 存储凭证
git credential-store store <<EOF
protocol=https
host=github.com
username=your_username
password=your_token
EOF

# 擦除凭证
git credential-store erase <<EOF
protocol=https
host=github.com
EOF

常见问题排查

问题 1:提交显示错误的用户名/邮箱

bash
# 检查当前配置
git config user.name
git config user.email

# 检查是否有项目级配置覆盖了全局配置
git config --local user.name
git config --local user.email

# 修正配置
git config --global user.name "Correct Name"
git config --global user.email "correct@email.com"

问题 2:修改历史提交的作者信息

bash
# 修改最后一次提交的作者
git commit --amend --author="New Name <new@email.com>"

# 批量修改所有提交的作者
git filter-branch --env-filter '
export GIT_AUTHOR_NAME="New Name"
export GIT_AUTHOR_EMAIL="new@email.com"
export GIT_COMMITTER_NAME="New Name"
export GIT_COMMITTER_EMAIL="new@email.com"
' --tag-name-filter cat -- --branches --tags

问题 3:SSH 密钥权限错误

bash
# 修复权限
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 644 ~/.ssh/known_hosts
chmod 600 ~/.ssh/config

问题 4:多个 SSH 密钥冲突

bash
# 使用 ssh-add 管理
ssh-add ~/.ssh/work_ed25519
ssh-add ~/.ssh/personal_ed25519

# 查看已添加的密钥
ssh-add -l

# 删除所有密钥
ssh-add -D

最佳实践

1. 使用专业邮箱

bash
# ✅ 推荐:使用专业邮箱
git config --global user.email "firstname.lastname@gmail.com"

# ❌ 避免:使用不专业的邮箱
git config --global user.email "coolguy123@hotmail.com"

2. 保持用户名一致

bash
# ✅ 推荐:使用真实姓名或固定笔名
git config --global user.name "John Doe"

# ❌ 避免:频繁更改用户名

3. 启用 GPG 签名

bash
# 生成 GPG 密钥
gpg --full-generate-key

# 配置 Git 使用 GPG
git config --global user.signingkey YOUR_KEY_ID
git config --global commit.gpgSign true

# 添加 GPG 密钥到 GitHub
cat ~/.gnupg/pubring.kbx | gpg --export --armor | pbcopy

4. 定期备份配置

bash
# 备份 Git 配置
cp ~/.gitconfig ~/.gitconfig.backup
cp -r ~/.ssh ~/.ssh.backup

# 或使用 Git 管理配置
mkdir ~/dotfiles
cp ~/.gitconfig ~/dotfiles/
cd ~/dotfiles
git init
git add .gitconfig
git commit -m "Backup Git config"

总结

正确配置 Git 用户信息至关重要:

  1. 基础配置:设置用户名和邮箱
  2. SSH 密钥:实现安全便捷的认证
  3. 多账户管理:灵活切换不同身份
  4. 凭证管理:安全存储访问令牌
  5. 最佳实践:保持专业性和一致性

下一步学习:

记住:良好的配置是高效使用 Git 的第一步!🎯