Git 配置与使用技巧
本文介绍 Git 的常用配置和实用技巧,包括 SSH 密钥生成、用户身份设置、代理配置以及提交历史修改等内容。
SSH 密钥配置
Section titled “SSH 密钥配置”生成 SSH 密钥
Section titled “生成 SSH 密钥”SSH 密钥用于安全地连接到 Git 仓库(如 GitHub、GitLab)。以下是生成 RSA 4096 位密钥的步骤:
Linux / macOS
Section titled “Linux / macOS”ssh-keygen -t rsa -b 4096 -C "your_email@example.com"Windows (PowerShell)
Section titled “Windows (PowerShell)”ssh-keygen -t rsa -b 4096 -C "your_email@example.com"密钥默认保存在 C:\Users\<YourUsername>\.ssh\id_rsa(Windows)或 ~/.ssh/id_rsa(Linux/macOS)。
配置 Git 用户身份
Section titled “配置 Git 用户身份”设置全局用户名和邮箱,用于提交记录:
git config --global user.name "Your Name"git config --global user.email "your_email@example.com"Generate a new SSH key (RSA 4096-bit):
Linux / macOS
Section titled “Linux / macOS”ssh-keygen -t rsa -b 4096 -C "your_email@example.com"Windows (PowerShell)
Section titled “Windows (PowerShell)”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.
User Identity
Section titled “User Identity”Set your global username and email:
Linux / macOS / Windows (Git Bash / PowerShell)
Section titled “Linux / macOS / Windows (Git Bash / PowerShell)”git config --global user.name "Your Name"git config --global user.email "your_email@example.com"Git 代理配置
Section titled “Git 代理配置”当需要通过代理访问 Git 仓库时,可以配置 Git 使用 HTTP 代理:
Linux / macOS
Section titled “Linux / macOS”git config --global http.proxy http://127.0.0.1:8888git config --global https.proxy http://127.0.0.1:8888Windows (PowerShell)
Section titled “Windows (PowerShell)”git config --global http.proxy http://127.0.0.1:8888git config --global https.proxy http://127.0.0.1:8888取消代理设置
Section titled “取消代理设置”git config --global --unset http.proxygit config --global --unset https.proxyWindows 特定配置
Section titled “Windows 特定配置”Windows 用户推荐以下配置:
# 自动处理行结束符(推荐)git config --global core.autocrlf true
# 使用 Windows 凭据管理器进行身份验证git config --global credential.helper manager
# 设置默认分支名称为 'main'git config --global init.defaultBranch main修改提交历史
Section titled “修改提交历史”修改所有提交的作者信息
Section titled “修改所有提交的作者信息”git filter-branch --env-filter 'export GIT_AUTHOR_EMAIL=new_email@example.com' --修改特定作者信息
Section titled “修改特定作者信息”将 Old@Email、Changed Name 和 Changed@Email 替换为实际值:
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"fiif [ "$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日志查询技巧
Section titled “日志查询技巧”按作者过滤日志
Section titled “按作者过滤日志”git log --author="Author Name"其他常用日志命令
Section titled “其他常用日志命令”# 显示简洁日志git log --oneline
# 显示图形化分支历史git log --graph --oneline --all
# 显示最近 10 条提交git log -10
# 显示指定文件的修改历史git log --follow filenameGitHub 文件历史可视化
Section titled “GitHub 文件历史可视化”将 GitHub 文件 URL 中的 .com 替换为 .githistory.xyz 可以查看文件的可视化历史:
# 原始 URLhttps://github.com/user/repo/blob/main/file.js
# 可视化历史 URLhttps://githistory.xyz/user/repo/blob/main/file.js- 提交规范:使用约定式提交规范提交信息
- 分支策略:采用 Git Flow 或 GitHub Flow 等分支管理策略
- 代码审查:使用 Pull Request 进行代码审查和协作