使用git rebase编辑合并提交 [英] Edit a merge commit with 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
只是一个自动化的方式来做大量的樱桃选择。您可以通过手动樱桃采摘来复制其行为,以更多地控制流程。这是不方便,更容易出现人为错误,但可能。
这是我建议的方法:
- 使用
git rebase
在(合并的子元素)之后到达 - 使用
git reset --hard HEAD ^
手动进行合并 - 使用
git commit --amend
修复合并 - 使用
git cherry-pick
返回合并后的提交 - 使用
git rebase --continue
完成
以下是具体步骤:
- 注意要修改的合并提交的SHA1 ID 。对于讨论,假设它是
deadbeef
。 - 注意你想要修改合并提交之后提交权限的SHA1 ID合并提交的孩子)。假设它是
facef00d
。 - 运行
git rebase -i deadbeef
/ li>
- 选择
facef00d
进行编辑。 - 当rebase返回编辑提示
facef00d
,运行git reset --hard HEAD ^
。你现在应该是deadbeef
(git rev-parse HEAD
应该打印deadbeef $
- 进行编辑以修复不正确的合并冲突,并使用
>git add
将它们分级。
- 运行
git commit --amend
来融合分阶段修复与错误的合并提交。结果现在将有不同的SHA1(不是deadbeef
)。
- 运行
git cherry-pick facef00d
将facef00d
所做的更改应用于固定合并提交。
- 运行
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 ofB
commit, - git rebase stops right after
B
commit so I can fix anything I want usinggit 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:
- use
git rebase
to get to the commit after the merge (the child of the merge) - use
git reset --hard HEAD^
to manually get to the merge - use
git commit --amend
to repair the merge - use
git cherry-pick
to get back to the commit after the merge - use
git rebase --continue
to finish
Here are the specific steps:
- Note the SHA1 ID of the merge commit you want to modify. For discussion, suppose it is
deadbeef
. - 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
. - Run
git rebase -i deadbeef
. - Select
facef00d
for editing. - When rebase returns you to a prompt to edit
facef00d
, rungit reset --hard HEAD^
. You should now be atdeadbeef
(git rev-parse HEAD
should printdeadbeef
). - Make your edits to fix the incorrect merge conflict and use
git add
to stage them. - Run
git commit --amend
to fuse the staged fix with the bad merge commit. The result will now have a different SHA1 (notdeadbeef
). - Run
git cherry-pick facef00d
to apply the changes made byfacef00d
to the fixed merge commit. - Run
git rebase --continue
to finish.
这篇关于使用git rebase编辑合并提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!