使用git rebase编辑合并提交 [英] Edit a merge commit with git rebase

查看:217
本文介绍了使用git rebase编辑合并提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Git中,当我提交例如。 A - B - C 我想编辑 B 提交,I




  • 使用 git rebase -i

  • 在列表中,我在 B commit,

  • edit 命令> git rebase在 B> commit之后停止,所以我可以使用 git commit --amend
  • ,然后我继续使用 git rebase --continue



<据我所知,这是如何做到这一点的最佳做法。有了这个方法,我可以编辑过去的任何提交(只要它还没有推送到远程分支),而且用 -p 标记我甚至可以保存合并。这是非常好的。



我现在的问题是:我在合并提交的一行中犯了一个错误(错字)(同时在合并两个分支时解决冲突)。



我想修正它,但我不知道如何让 git rebase 停在a合并提交。 git rebase -p -i > 列表会忽略合并提交,所以我不能在前面写 edit 命令并且让 git rebase 停在那里让我编辑它。



有什么帮助吗?
我只想在合并提交中修复此行,同时保留所有提交(和合并)后。



谢谢。

解决方案

当涉及到合并时,Git并没有很容易做到交互式分配。 -p 选项在内部使用 -i 机制,所以混合两者并不会真正起作用。



然而, git rebase 只是一个自动化的方式来做大量的樱桃选择。您可以通过手动樱桃采摘来复制其行为,以更多地控制流程。这是不方便,更容易出现人为错误,但可能。



这是我建议的方法:


  1. 使用 git rebase (合并的子元素)之后到达

  2. 使用 git reset --hard HEAD ^ 手动进行合并

  3. 使用 git commit --amend 修复合并

  4. 使用 git cherry-pick 返回合并后的提交

  5. 使用 git rebase --continue 完成

以下是具体步骤:


  1. 注意要修改的合并提交的SHA1 ID 。对于讨论,假设它是 deadbeef

  2. 注意你想要修改合并提交之后提交权限的SHA1 ID合并提交的孩子)。假设它是 facef00d

  3. 运行 git rebase -i deadbeef / li>
  4. 选择 facef00d 进行编辑。

  5. 当rebase返回编辑提示 facef00d ,运行 git reset --hard HEAD ^ 。你现在应该是 deadbeef git rev-parse HEAD 应该打印 deadbeef
  6. 进行编辑以修复不正确的合并冲突,并使用 git add 将它们分级。
  7. >
  8. 运行 git commit --amend 来融合分阶段修复与错误的合并提交。结果现在将有不同的SHA1(不是 deadbeef )。

  9. 运行 git cherry-pick facef00d facef00d 所做的更改应用于固定合并提交。

  10. 运行 git rebase - 继续完成。


In Git when I have commits eg. A - B - C and I want to edit the B commit, I

  • use git rebase -i <A-commit-hash>,
  • in the list I write edit command in front of B commit,
  • git rebase stops right after B commit so I can fix anything I want using git commit --amend,
  • and then I continue using git rebase --continue.

As far as I know this is the best practice how to do this. With this method I can edit any commit in the past (as long as it hasn't been pushed to remote branch yet), and moreover with -p flag I can even preserve the merges. This is just great.

My current problem is: I did a mistake (typo) on one line in a merge commit (while resolving a conflict when merging two branches).

I'd like to fix it but I don't know how to make git rebase to stop at a merge commit. The git rebase -p -i <blah> list ignores merge commits, so I cannot write edit command in front of it and make the git rebase stop there to let me edit it.

Any help please? I just want to fix this line in the merge commit while preserving all the commits (and merges) after it.

Thanks.

解决方案

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.

这篇关于使用git rebase编辑合并提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆