修改一个历史提交的父提交


在半年前,合并热修复分支时,合并方向弄反了,导致热修复分支成为了新的主分支。但一直没有什么办法,今天重拾起来,尝试去修复。

参考资料

  1. How do I swap the order of two parents of a Git commit?(引子)
  2. How do git grafts and replace differ? (Are grafts now deprecated?)
  3. Setting git parent pointer to a different parent(解决方法出处)

效果预览

  • 修复前

修复前的 Git 提交树

  • 修复后

修复后的 Git 提交树

操作步骤

备份项目文件(打包一个压缩包)

创建一个嫁接提交

切换(修改)目标分支的信息(包括父分支等)(参考出处

git replace --graft <目标 commit> <主分支的父节> <热修复分支的父节>

检查修改

检查嫁接 commit 的信息是否正确,符合预期(现在还没修改到原 commit 上,只是保存文件至 .git/refs/replace/)

查看原 commit 信息

git cat-file commit <目标 commit>

查看嫁接 commit 信息

git --no-replace-objects cat-file commit <目标 commit>

覆盖原 commit

安装 git-filter-repo(需要 git 在 2.22 版本以上)

覆盖提交(参考出处

git filter-repo --force

重新绑定远端仓库

git remote add origin <你的远端仓库地>

提交

提交修改(每个分支都要)

git push -f <分支>

告知项目相关者

告知一同开发的开发者保存现有代码并重新 pull 项目 🙂