Skip to content

Git 配置与使用技巧

本文介绍 Git 的常用配置和实用技巧,包括 SSH 密钥生成、用户身份设置、代理配置以及提交历史修改等内容。

SSH 密钥用于安全地连接到 Git 仓库(如 GitHub、GitLab)。以下是生成 RSA 4096 位密钥的步骤:

Terminal window
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Terminal window
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

密钥默认保存在 C:\Users\<YourUsername>\.ssh\id_rsa(Windows)或 ~/.ssh/id_rsa(Linux/macOS)。

设置全局用户名和邮箱,用于提交记录:

Terminal window
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

Generate a new SSH key (RSA 4096-bit):

Terminal window
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Terminal window
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

The key will be saved to C:\Users\<YourUsername>\.ssh\id_rsa by default.

Set your global username and email:

Linux / macOS / Windows (Git Bash / PowerShell)

Section titled “Linux / macOS / Windows (Git Bash / PowerShell)”
Terminal window
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

当需要通过代理访问 Git 仓库时,可以配置 Git 使用 HTTP 代理:

Terminal window
git config --global http.proxy http://127.0.0.1:8888
git config --global https.proxy http://127.0.0.1:8888
Terminal window
git config --global http.proxy http://127.0.0.1:8888
git config --global https.proxy http://127.0.0.1:8888
Terminal window
git config --global --unset http.proxy
git config --global --unset https.proxy

Windows 用户推荐以下配置:

Terminal window
# 自动处理行结束符(推荐)
git config --global core.autocrlf true
# 使用 Windows 凭据管理器进行身份验证
git config --global credential.helper manager
# 设置默认分支名称为 'main'
git config --global init.defaultBranch main
Terminal window
git filter-branch --env-filter 'export GIT_AUTHOR_EMAIL=new_email@example.com' --

Old@EmailChanged NameChanged@Email 替换为实际值:

Terminal window
git filter-branch -f --env-filter '
OLD_EMAIL="Old@Email"
CORRECT_NAME="Changed Name"
CORRECT_EMAIL="Changed@Email"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
Terminal window
git log --author="Author Name"
Terminal window
# 显示简洁日志
git log --oneline
# 显示图形化分支历史
git log --graph --oneline --all
# 显示最近 10 条提交
git log -10
# 显示指定文件的修改历史
git log --follow filename

将 GitHub 文件 URL 中的 .com 替换为 .githistory.xyz 可以查看文件的可视化历史:

# 原始 URL
https://github.com/user/repo/blob/main/file.js
# 可视化历史 URL
https://githistory.xyz/user/repo/blob/main/file.js
  • 提交规范:使用约定式提交规范提交信息
  • 分支策略:采用 Git Flow 或 GitHub Flow 等分支管理策略
  • 代码审查:使用 Pull Request 进行代码审查和协作