简体   繁体   English

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

[英]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.

相关问题 如何将3次提交与主服务器的一次合并重新定为一次提交? - How can I rebase 3 commits with one merge of master into one commit? 如何在不解决其他提交冲突的情况下在 git 中变基,或者压缩我的所有提交,同时不影响其他人的提交? - How can I rebase in git without resolving other commit conflicts, or squash all of my commits while leaving others' commits untouched? Git提交压缩(rebase -i):提交实际上如何工作? - Git commit squashing (rebase -i): how do commits actually work? 恢复后如何在保留提交信息的同时提交相同的提交? - How to commit the same commits after reverting, while preserving commit information? 如何在合并提交之前 Git 以交互方式变基压缩少量提交 - How to Git rebase interractively squash few commits before merge commit 如何使用rebase而不是合并来拉取所有远程更改? - How can I pull all remote changes with rebase instead of merge? 我可以恢复到旧的 Git 提交,同时保留最新的提交吗? - Can I revert to an old Git commit, while preserving the latest commits? 如何在跳过特定提交时重新绑定? - How do I rebase while skipping a particular commit? 如何将所有先前的提交合并为一个提交? - How to merge all previous commits into a single commit? 如何通过git rebase --interactive编辑提交时保留提交消息? - How do I keep the commit message when editing commits via git rebase --interactive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM