簡體   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