[英]How do I rebase merge commits while preserving the commit IDs of all the changes
Let's say I maintain a fork of a project.假设我维护一个项目的分支。
I want to perdiodically rebase downstream onto upstream while preserving all the commit SHAs of the changes.我想定期将下游重新定位到上游,同时保留更改的所有提交 SHA。 That is, I want to rebase each individual merge commit , but I want all the change commits to keep their own parent.也就是说,我想重新设置每个单独的合并提交,但我希望所有更改提交都保留它们自己的父级。
Here's a simplified example, with only one downstream patch.这是一个简化的示例,只有一个下游补丁。
Initial state:初始 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
Now, I run:现在,我运行:
$ git rebase --rebase-merges=no-rebase-cousins upstream
I end up with:我最终得到:
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
Instead, this is I want ( notice that "Downstream commit 1" still has "Upstream commit 2" as a parent , hence it keeps its commit ID):相反,这是我想要的(注意“下游提交 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
Is there a magic trick to end up in the desired state?是否有一个魔术技巧可以最终出现在所需的 state 中? (one that ideally works when there are N downstream merge commits to rebase) (理想情况下,当有 N 个下游合并提交要变基时)
I want to perdiodically rebase downstream onto upstream while preserving all the commit SHAs of the changes.我想定期将下游重新定位到上游,同时保留更改的所有提交 SHA。
This is not possible.这是不可能的。 The SHA hash of a commit is computed from the contents of the commit which include its parents.提交的 SHA hash 是根据提交的内容(包括其父项)计算得出的。 If you rebase the downstream branch onto an upstream branch, the parent of the first rebase changes which creates a new commit with a new hash.如果您将下游分支变基到上游分支,则第一个变基的父级会更改,从而创建具有新 hash 的新提交。 Then the next commit must be rebased onto this new parent so its hash changes, etc.然后下一次提交必须重新基于这个新的父级,以便它的 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
From this diagram, it seems like you need to do a merge, not a rebase.从这个图中,您似乎需要进行合并,而不是变基。 For example:例如:
git checkout downstream
git merge 732985e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.