git git从历史记录中删除合并提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17577409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:31:32  来源:igfitidea点击:

git remove merge commit from history

gitgit-mergegit-rebase

提问by Benjamin Toueg

My Git history looks like that :

我的 Git 历史看起来像这样:

Git history

Git 历史

I would like to squash the purple commits into a single one. I don't want to see them ever again in my commit log.

我想将紫色提交压缩为一个。我不想在我的提交日志中再次看到它们。

I've tried to do a git rebase -i 1, but even though 1is on the blue branch (cf. picture), I still see every commit on my purple branch.

我试图做一个git rebase -i 1,但即使1在蓝色分支上(参见图片),我仍然看到我的紫色分支上的每个提交。

How can I completely remove the purple branch (from commit log) ?

如何完全删除紫色分支(从提交日志中)?

回答by Schleis

Do git rebase -i <sha before the branches diverged>this will allow you to remove the merge commit and the log will be one single line as you wanted. You can also delete any commits that you do not want any more. The reason that your rebase wasn't working was that you weren't going back far enough.

这样做git rebase -i <sha before the branches diverged>将允许您删除合并提交,并且日志将是您想要的一行。您还可以删除不再需要的任何提交。你的 rebase 不起作用的原因是你回溯的不够远。

WARNING: You are rewriting history doing this. Doing this with changes that have been pushed to a remote repo will cause issues. I recommend only doing this with commits that are local.

警告:你正在重写历史这样做。对已推送到远程存储库的更改执行此操作会导致问题。我建议只对本地提交执行此操作。

回答by Allen Luce

Starting with the repo in the original state

从原始状态的 repo 开始

Original repo history

原始回购历史

To remove the merge commit and squash the branch into a single commit in the mainline

删除合并提交并将分支压缩为主线中的单个提交

Squashed commits, no merge commit

压扁提交,没有合并提交

Use these commands (replacing 5 and 1 with the SHAs of the corresponding commits):

使用这些命令(用相应提交的 SHA 替换 5 和 1):

git checkout 5
git reset --soft 1
git commit --amend -m '1 2 3 4 5'
git rebase HEAD master

To retain a merge commit but squash the branch commits into one:

要保留合并提交但将分支提交压缩为一个:

Squashed commits, retained merge commit

压扁提交,保留合并提交

Use these commands (replacing 5, 1 and C with the SHAs of the corresponding commits):

使用这些命令(用相应提交的 SHA 替换 5、1 和 C):

git checkout -b tempbranch 5
git reset --soft 1
git commit --amend -m '1 2 3 4 5'
git checkout C
git merge --no-ff tempbranch
git rebase HEAD master

To remove the merge commit and replace it with individual commits from the branch

删除合并提交并将其替换为分支中的单个提交

Branch moved into mainline, no merge commit

分支移入主线,无合并提交

Just do (replacing 5 with the SHA of the corresponding commit):

只需执行(用相应提交的 SHA 替换 5):

git rebase 5 master

And finally, to remove the branch entirely

最后,完全删除分支

Branch removed entirely

分支完全删除

Use this command (replacing C and D with the SHAs of the corresponding commits):

使用此命令(用相应提交的 SHA 替换 C 和 D):

git rebase --onto C D~ master

回答by prem

There are two ways to tackle this based on what you want:

根据您的需要,有两种方法可以解决此问题:

Solution 1: Remove purple commits, preserving history (incase you want to roll back)

解决方案 1:删除紫色提交,保留历史记录(以防您想回滚)

git revert -m 1 <SHA of merge>

-m 1specifies which parent line to choose

-m 1指定要选择的父行

Purple commits will still be there in history but since you have reverted, you will not see code from those commits.

紫色提交仍将存在于历史记录中,但由于您已恢复,因此您将看不到这些提交的代码。



Solution 2: Completely remove purple commits (disruptive change if repo is shared)

解决方案 2:完全删除紫色提交(如果 repo 是共享的,则破坏性更改)

git rebase -i <SHA before branching out>

and delete (remove lines) corresponding to purple commits.

并删除(删除行)对应于紫色提交。

This would be less tricky if commits were not made after merge. Additional commits increase the chance of conflicts during revert/rebase.

如果在合并后未进行提交,这将不那么棘手。额外的提交会增加revert/rebase.

回答by Warren

To Just Remove a Merge Commit

仅删除合并提交

If all you want to do is to remove a merge commit (2) so that it is like it never happened, the command is simply as follows

如果你只想删除一个合并提交(2),让它就像从来没有发生过一样,命令如下

git rebase --onto <sha of 1> <sha of 2> <blue branch>

git rebase --onto <sha of 1> <sha of 2> <blue branch>

And now the purple branch isn't in the commit log of blue at all and you have two separate branches again. You can then squash the purple independently and do whatever other manipulations you want without the merge commit in the way.

现在紫色分支根本不在蓝色的提交日志中,您又拥有两个独立的分支。然后,您可以独立地压缩紫色,并在不妨碍合并提交的情况下执行您想要的任何其他操作。