简体   繁体   English

如何在没有合并冲突的情况下将一长串 git 合并转换为单个 rebase?

[英]How can I convert a long series of git merges into a single rebase without merge conflicts?

So I have a branch of master, let's call it foo, that I've been using for a while, and after about 50 commits or so I was getting a rather complicated merge history, as foo had some subbranches of its own.所以我有一个 master 分支,我们称之为 foo,我已经使用了一段时间,在大约 50 次提交之后,我得到了一个相当复杂的合并历史,因为 foo 有一些自己的子分支。 The history was not important to me here, so I decided to clean things up by rebasing each branch and squashing all the commits down to one, so that I'd have just one commit representing the difference between master and that branch.历史在这里对我来说并不重要,所以我决定通过重新设置每个分支并将所有提交压缩为一个来清理东西,这样我就只有一个提交代表 master 和那个分支之间的差异。

At first I thought I could just do:起初我以为我可以这样做:

git checkout foo
git rebase master

but this didn't work out for me.但这对我不起作用。 The branch had over 50 commits, each touching many files, and each commit was getting a bunch of conflicts.该分支有 50 多个提交,每个提交都涉及许多文件,并且每次提交都会遇到一堆冲突。

Instead, what I ended up doing was this:相反,我最终做的是这样的:

git checkout foo
<copy all files to another folder>
git checkout master
git branch -D foo
git checkout -b foo
<overwrite all files with the copy I made earlier, and create a new commit>

This served my needs of turning the long history of merges into a single squashed rebase without dealing with all the conflicts along the way, but I'm just wondering if there was a more "git-friendly" way of doing this?这满足了我将合并的悠久历史变成一个单一的压缩变基而不处理一路上的所有冲突的需求,但我只是想知道是否有一种更“git-friendly”的方式来做到这一点?

The friendlier way of doing this in git is to make a 3rd integration branch.在 git 中更友好的方法是创建第三个集成分支。 As you develop foo, merge master and foo on that.在开发 foo 时,将 master 和 foo 合并。 This will show git how you are dealing with the code bases changing at the same time.这将显示 git 如何处理同时更改的代码库。 It is important to have rerere (reuse recorded resolution) enabled in your config.在您的配置中启用 rerere(重用记录的分辨率)很重要。

Now when you are finally ready to merge foo into master, git will know how to resolve the conflicts as you were telling it how to do that all along.现在,当您终于准备好将 foo 合并到 master 时,git 将知道如何解决冲突,就像您一直告诉它如何做到这一点一样。

Or, just merge that integration branch into master when ready.或者,在准备好后将该集成分支合并到 master 中。 You should have no conflicts.你应该没有冲突。 It depends if you can tolerate all those test merges in your history.这取决于您是否可以容忍历史记录中的所有这些测试合并。

This is somewhat related to my post on Branch per Feature: http://dymitruk.com/blog/2012/02/05/branch-per-feature/这与我在 Branch per Feature 上的帖子有些相关: http : //dymitruk.com/blog/2012/02/05/branch-per-feature/

I don't recommend squashing commits as you lose information on how files came to be in the state that they are in. The more information that git has to work with, the better.我不建议压缩提交,因为您会丢失有关文件如何处于它们所处状态的信息。git 必须处理的信息越多越好。

Have you tried:你有没有尝试过:

git merge --squash

What this does is it takes all the changes and puts them into the index ready to be committed.它的作用是获取所有更改并将它们放入准备提交的索引中。 You'll still have merge conflicts, but they will be in one place and you can fix them before commiting the stated in one big chunk.您仍然会遇到合并冲突,但它们会在一个地方,您可以在提交一大块声明之前修复它们。

For a more detailed explanation and diagrams have a look at this有关更详细的解释和图表,请查看

Git's porcelain can't really handle the contortions necessary to truly squash all the merge steps into one octopus merge commit. Git 的瓷器无法真正处理将所有合并步骤真正压缩为一个章鱼合并提交所需的扭曲。 Luckily, Git exposes the plumbing required so that you can do this with one command.幸运的是,Git 公开了所需的管道,因此您可以使用一个命令完成此操作。 See this answer to a question I had about octopus merges .请参阅我对 octopus merges 的问题的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 git rebase --preserve-merges 合并冲突 - git rebase --preserve-merges with merge conflicts 如何强制 git 变基并覆盖合并冲突 - How can I force a git rebase and override merge conflicts 如何合并一个只导致单个提交的git分支(比如使用--squash),但允许将来的合并没有冲突? - How to merge a git branch that results in only a single commit (like using --squash), but allowing future merges without conflicts? Git可以在重新设置基准期间将合并转换为南瓜吗? - Can Git convert merges to squashes during rebase? 如何使用 git rebase -i --rebase-merges 命令压缩合并提交和正常提交 - How to squash merge commits and normal commits with git rebase -i --rebase-merges command 如何找到Git存储库中存在冲突的所有合并? - How can I find all the merges that had conflicts in a Git repository? Git:如何修改旧的合并(在一系列合并中)? - Git: how to amend an old merge (in a series of merges)? 如何编辑包含数百次合并的 Git 提交并将冲突合并到过去? - How do I edit a Git commit that is hundreds of merges and merge conflicts into the past? 使用git rebase -i压缩单个分支上的提交,遇到合并冲突 - Using git rebase -i to squash commits on single branch, encountering merge conflicts Git颜色合并/ rebase冲突 - Git color merge/rebase conflicts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM