临时修补前一个提交时,可以用 git commit --fixup=<commit> 标记它,最后 rebase 时让 Git 自动把修补内容合并回原提交。
环境:Git 2.34+。简单试一下:
git init -q fixup-demo && cd fixup-demo
git config user.email [email protected]
git config user.name dev
printf 'one\ntwo\n' > note.txt
git add note.txt && git commit -qm 'add note'
printf 'three\n' >> note.txt
git add note.txt && git commit -qm 'add more'
# 假设要把当前改动修回上一个提交
printf 'two-fixed\n' > note.txt
git add note.txt
git commit --fixup=HEAD
git log --oneline --decorate -3
# 从根提交开始整理
git rebase -i --autosquash --root
编辑器里会看到 fixup! 提交已经排在目标提交后面,保存后 Git 会自动合并。共享分支上操作前先确认提交还没被别人基于它继续开发。