繁体   English   中英

如何在保留所有更改的提交 ID 的同时重新合并提交

[英]How do I rebase merge commits while preserving the commit IDs of all the changes

假设我维护一个项目的分支。

我想定期将下游重新定位到上游,同时保留更改的所有提交 SHA。 也就是说,我想重新设置每个单独的合并提交,但我希望所有更改提交都保留它们自己的父级。

这是一个简化的示例,只有一个下游补丁。

初始 state:

DOWNSTREAM:
*   d9ce4cb Merge branch 'pr1' into downstream   # <- our merge commit for the downstream change
|\
| * dcc0f5e (pr1) Downstream commit 1       # <- our change downstream
|/
* ee6faa1 Upstream commit 2
* c57fe05 Upstream commit 1
UPSTREAM:
* 732985e Upstream commit 3       # <- one NEW COMMIT upstream
* ee6faa1 Upstream commit 2
* c57fe05 Upstream commit 1

现在,我运行:

$ git rebase --rebase-merges=no-rebase-cousins upstream

我最终得到:

ACTUAL DOWNSTREAM:
*   e7574a0 Merge branch 'pr1' into downstream
|\
| * 2dc0049 (pr1) Downstream commit 1       # <-- BAD: The downstream commit has changed parent
|/
* 732985e Upstream commit 3
* ee6faa1 Upstream commit 2
* c57fe05 Upstream commit 1

相反,这是我想要的(注意“下游提交 1”仍然有“上游提交 2”作为父级,因此它保留其提交 ID):

DESIRED NEW DOWNSTREAM:
*   e7574a0 Merge branch 'pr1' into downstream      # <- The merge commit has moved
|\
* | 732985e Upstream commit 3
| * dcc0f5e (pr1) Downstream commit 1       # <- GOOD: The downstream commit is preserved
|/
* ee6faa1 Upstream commit 2
* c57fe05 Upstream commit 1

是否有一个魔术技巧可以最终出现在所需的 state 中? (理想情况下,当有 N 个下游合并提交要变基时)

我想定期将下游重新定位到上游,同时保留更改的所有提交 SHA。

这是不可能的。 提交的 SHA hash 是根据提交的内容(包括其父项)计算得出的。 如果您将下游分支变基到上游分支,则第一个变基的父级会更改,从而创建具有新 hash 的新提交。 然后下一次提交必须重新基于这个新的父级,以便它的 hash 发生变化,等等。

 DESIRED NEW DOWNSTREAM: * e7574a0 Merge branch 'pr1' into downstream # <- The merge commit has moved |\ * | 732985e Upstream commit 3 | * dcc0f5e (pr1) Downstream commit 1 # <- GOOD: The downstream commit is preserved |/ * ee6faa1 Upstream commit 2 * c57fe05 Upstream commit 1

从这个图中,您似乎需要进行合并,而不是变基。 例如:

git checkout downstream
git merge 732985e

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM