摘要:大多数人只会git add/commit/push三板斧。本文分享10个进阶Git技巧,包括交互式rebase、bisect定位bug、stash管理、reflog救命等,让你的Git操作从"能用"变成"好用"。
技巧1:交互式暂存(部分提交)
一个文件改了多处,但想分成多次提交:
# 交互式选择要暂存的代码块
git add -p
# 对每个代码块,Git会问你:
# y - 暂存这个块
# n - 跳过
# s - 把这个块拆得更小
# e - 手动编辑
实际场景:你在一个文件里同时修了bug和加了新功能,用git add -p把它们分成两次提交,保持提交历史清晰。
技巧2:交互式Rebase
整理提交历史的神器:
# 整理最近5次提交
git rebase -i HEAD~5
编辑器会显示:
pick abc1234 添加用户注册功能
pick def5678 修复注册bug
pick ghi9012 注册功能补充验证
pick jkl3456 更新README
pick mno7890 修复typo
常用操作:
squash(s):合并到上一个提交reword(r):修改提交信息drop(d):删除提交edit(e):暂停让你修改- 调整行顺序:改变提交顺序
pick abc1234 添加用户注册功能
squash def5678 修复注册bug
squash ghi9012 注册功能补充验证
reword jkl3456 更新README
drop mno7890 修复typo
这样3个注册相关的提交合并成1个,typo提交直接删掉。
技巧3:Stash高级用法
# 基础:暂存当前修改
git stash
# 带描述信息(推荐)
git stash push -m "正在做的用户模块"
# 只暂存部分文件
git stash push -m "临时保存" -- path/to/file1 path/to/file2
# 暂存包括未跟踪的文件
git stash -u
# 查看所有stash
git stash list
# 查看某个stash的内容
git stash show -p stash@{0}
# 应用但不删除
git stash apply stash@{1}
# 应用并删除
git stash pop
# 从stash创建分支(避免冲突)
git stash branch new-feature stash@{0}
技巧4:Bisect二分查找Bug
知道某个版本是好的,当前版本有bug,用bisect快速定位:
git bisect start
git bisect bad # 当前版本有bug
git bisect good v1.0 # v1.0是好的
# Git会自动checkout中间的提交,你测试后告诉它:
git bisect good # 这个版本没bug
git bisect bad # 这个版本有bug
# 重复几次后,Git会告诉你哪个提交引入了bug
# 结束
git bisect reset
自动化版本(用脚本测试):
git bisect start HEAD v1.0
git bisect run python test_script.py
# Git自动运行脚本,根据退出码判断good/bad
技巧5:Reflog——后悔药
误操作后的救命稻草:
# 查看所有操作历史(包括已删除的提交)
git reflog
# 输出类似:
# abc1234 HEAD@{0}: reset: moving to HEAD~3
# def5678 HEAD@{1}: commit: 重要的提交
# ghi9012 HEAD@{2}: commit: 另一个提交
# 恢复到误删前的状态
git reset --hard def5678
# 或者只恢复某个提交
git cherry-pick def5678
记住:只要提交过,Git几乎不会真正丢失数据。reflog默认保留90天。
技巧6:Worktree多分支并行开发
不用来回切换分支:
# 在另一个目录checkout不同分支
git worktree add ../hotfix-branch hotfix/urgent-fix
git worktree add ../feature-branch feature/new-ui
# 查看所有worktree
git worktree list
# 删除worktree
git worktree remove ../hotfix-branch
场景:正在开发新功能,突然要修紧急bug。用worktree在另一个目录修bug,不影响当前工作。
技巧7:高级日志查看
# 图形化分支历史
git log --oneline --graph --all
# 搜索提交信息
git log --grep="修复" --oneline
# 搜索代码变更(谁改了这个函数?)
git log -S "def calculate_price" --oneline
# 查看某个文件的修改历史
git log --follow -p -- path/to/file
# 查看某人的提交
git log --author="张三" --since="2026-01-01" --oneline
# 统计每人提交数
git shortlog -sn --no-merges
技巧8:Cherry-pick精准移植
从其他分支摘取特定提交:
# 摘取单个提交
git cherry-pick abc1234
# 摘取多个提交
git cherry-pick abc1234 def5678
# 摘取一个范围(不包含起点)
git cherry-pick abc1234..ghi9012
# 只应用变更,不自动提交
git cherry-pick --no-commit abc1234
# 冲突时
git cherry-pick --continue # 解决冲突后继续
git cherry-pick --abort # 放弃
技巧9:Git Aliases提效
在~/.gitconfig中添加:
[alias]
st = status -sb
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --all --decorate
unstage = reset HEAD --
last = log -1 HEAD --stat
diff-staged = diff --cached
amend = commit --amend --no-edit
wip = !git add -A && git commit -m 'WIP'
undo = reset --soft HEAD~1
cleanup = !git branch --merged | grep -v '\\*\\|main\\|master' | xargs -n 1 git branch -d
使用:
git st # 简洁状态
git lg # 漂亮的日志
git amend # 追加到上次提交
git wip # 快速保存工作进度
git undo # 撤销上次提交(保留修改)
git cleanup # 删除已合并的分支
技巧10:Git Hooks自动化
.git/hooks/pre-commit(提交前自动检查):
#!/bin/bash
# 运行代码格式化
echo "Running black..."
black --check . 2>/dev/null
if [ $? -ne 0 ]; then
echo "❌ 代码格式不符合规范,请运行 black ."
exit 1
fi
# 运行lint
echo "Running flake8..."
flake8 . --count --select=E9,F63,F7,F82 --show-source
if [ $? -ne 0 ]; then
echo "❌ 代码有语法错误"
exit 1
fi
# 检查是否有调试代码
if grep -rn "import pdb\|breakpoint()\|print(" --include="*.py" .; then
echo "⚠️ 检测到调试代码,确认要提交吗?"
read -p "继续?(y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
echo "✅ 检查通过"
chmod +x .git/hooks/pre-commit
推荐用pre-commit框架管理hooks:
pip install pre-commit
.pre-commit-config.yaml:
repos:
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
pre-commit install
总结
Git的进阶操作核心在于:
rebase -i整理提交历史stash灵活管理工作进度bisect高效定位bugreflog是你的后悔药worktree并行开发不切分支aliases和hooks提升日常效率
这些技巧不需要一次全学会,遇到具体场景时翻出来用,用几次就记住了。