首页 > 解决方案 > How to create a proper merge commit in git which is also squashed

问题描述

I'm trying to solve a problem with my git history.

I have two branches, lets call them a and b, a is the source branch for my repository, and b was branched off it. Many merged pull requests (a mix of merge commits, and squashed commits) happen to a, and b is not rebased, neither are these PRs re-opened against b. In order to update b, I (naively) resort to cherry-picking the merge commits from a that are applicable to b.

Over time as the code is tested and approved in a, b is now equal to a, with an empty diff, but the history shows, (as expected) a divergence in commits. (b is seen as many commits ahead of a in Github).

I would like to eliminate this divergence, if I perform merge commits from either one into the other, I get a long string of commits with no changes (except in some cases on Github where it gets confused), which would pollute the history of either branch if allowed onto them.

e.g. git merge a from b or git merge b from a shows a long string of commits in the log. (Edit: this is with --no-ff).

If I perform a squash, I get a nice message listing those commits with no changes.

e.g. git merge a --squash from b or git merge b --squash from a requires git commit --allow-empty.

But that does not prevent the "this branch is ahead/behind" situation.

So my question is how do I produce a single commit (can be in either or both branches) to prevent the "ahead/behind" situation.

Edit: This is mainly for ease of tracing whats happened on Github, I know how git works and know how to do this by destroying the history of one or the other branches by force pushing, but I'm trying to preserve both in a way that is easily introspectable.

标签: gitgithubmerge

解决方案


诉诸樱桃采摘[...]随着时间的推移,随着代码在 中的测试和批准ab现在等于 a,差异为空,但历史显示,(如预期的那样)提交分歧。我想消除这种分歧,如果我从一个执行合并提交到另一个,我会得到一长串提交 [...],这会污染历史

您要么在历史记录中有不同的提交,要么有相同的提交。你不能两全其美。

但是您可以很容易地不列出合并的提交。 git log --first-parent仅向您显示您的主要祖先的提交,而不列出任何合并的历史详细信息。这可能就是你想要的。将一长串提交折叠成一个摘要合并提交,然后可以单独列出,这--first-parent是一个有用的技巧。查找--no-ff合并,它们是您记录摘要合并的方式,将合并的历史记录放在第二个父分支上,而不是简单地快速转发整个列表,将其作为主线的一部分。


推荐阅读