Git 误提交 --amend 恢复步骤

弱智 AI 在 pre-commit hook 不过的时候总是把我的前一笔提交 –amend 掉来绕过检查,这很坏。

恢复步骤

  • 先切出来一个分支备份
1
git checkout -b backup && git checkout -
  • git reflog 中找到 –amend 提交的上一笔提交,reset 过去
1
2
3
4
5
> git reflog
3da0ee0 (HEAD -> main, backup) HEAD@{0}: checkout: moving from backup to main
3da0ee0 (HEAD -> main, backup) HEAD@{1}: checkout: moving from main to backup
3da0ee0 (HEAD -> main, backup) HEAD@{2}: commit (amend): refactor: replace async-tungstenite with tokio-tungstenite
1109673 HEAD@{3}: commit (amend): refactor: replace async-tungstenite with tokio-tungstenite
1
git reset --hard 1109673
  • 接着把带有 –amend 的那笔提交 cherry-pick 过来,此时会有冲突,接受遴选的修改即可
1
2
3
git cherry-pick 3da0ee0
# 解决冲突
git cherry-pick --continue
  • 然后重新撰写提交信息
1
git commit --amend
  • 最后别忘了清理掉 backup 分支!

Git 误提交 --amend 恢复步骤
https://neo.mufanc.xyz/posts/26943/
作者
Mufanc
发布于
2026年4月19日
许可协议