跳轉到內容

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 的第一步!🎯

最後更新於: