git - 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.
解决方案
诉诸樱桃采摘[...]随着时间的推移,随着代码在 中的测试和批准
a
,b
现在等于 a,差异为空,但历史显示,(如预期的那样)提交分歧。我想消除这种分歧,如果我从一个执行合并提交到另一个,我会得到一长串提交 [...],这会污染历史
您要么在历史记录中有不同的提交,要么有相同的提交。你不能两全其美。
但是您可以很容易地不列出合并的提交。 git log --first-parent
仅向您显示您的主要祖先的提交,而不列出任何合并的历史详细信息。这可能就是你想要的。将一长串提交折叠成一个摘要合并提交,然后可以单独列出,这--first-parent
是一个有用的技巧。查找--no-ff
合并,它们是您记录摘要合并的方式,将合并的历史记录放在第二个父分支上,而不是简单地快速转发整个列表,将其作为主线的一部分。
推荐阅读
- android - 更新后如何在最新的 Firebase 存储中上传和检索图像
- python - 不确定为什么合并时出现关键错误
- spring - 找不到 postgresql jdbc 驱动程序
- android - ImageView android:cropToPadding,它实际上是做什么的?
- chisel - 在测试仪中使用多个时钟
- excel - 对于范围内的每个单元格,总计下一列单元格
- css - R DT 覆盖默认选择颜色
- api - Alexa Proactive Events API - 如何确定用户是否禁用了 Alexa 通知?
- javascript - Javascript,从值创建一个数组
- swift - 如何随着时间的推移或分数更新时增加重力?