Edit a merge commit with git rebase

◇◆丶佛笑我妖孽 提交于 2019-11-28 15:43:14

Git does not make it easy to do interactive rebases when merges are involved. The -p option uses the -i mechanism internally, so mixing the two doesn't really work.

However, git rebase is just an automated way to do lots of cherry-picks. You can replicate its behavior by manually cherry-picking to get a bit more control over the process. It's less convenient and more prone to human error, but possible.

This is the approach I suggest:

  1. use git rebase to get to the commit after the merge (the child of the merge)
  2. use git reset --hard HEAD^ to manually get to the merge
  3. use git commit --amend to repair the merge
  4. use git cherry-pick to get back to the commit after the merge
  5. use git rebase --continue to finish

Here are the specific steps:

  1. Note the SHA1 ID of the merge commit you want to modify. For discussion, suppose it is deadbeef.
  2. Note the SHA1 ID of the commit right after the merge commit you want to modify (the merge commit's child). Suppose it is facef00d.
  3. Run git rebase -i deadbeef.
  4. Select facef00d for editing.
  5. When rebase returns you to a prompt to edit facef00d, run git reset --hard HEAD^. You should now be at deadbeef (git rev-parse HEAD should print deadbeef).
  6. Make your edits to fix the incorrect merge conflict and use git add to stage them.
  7. Run git commit --amend to fuse the staged fix with the bad merge commit. The result will now have a different SHA1 (not deadbeef).
  8. Run git cherry-pick facef00d to apply the changes made by facef00d to the fixed merge commit.
  9. Run git rebase --continue to finish.

May be easier to create a fixup commit 'D' then use 'git rebase -p -i <blah>' to reorder 'D' right after 'B' and squash it into 'B'.

pick A
pick B  <- merge commit to ammend
fixup D
pick C
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!