--preserve-合并当前分支中所有提交的交互式rebase

bar*_*s2x 2 git rebase git-rewrite-history

似乎已经有一些类似的问题,但是这些都不是我想要的。

假设我有这样的提交历史

* xxxxxxxH (HEAD -> B) Latest commit
* (more commits)
* xxxxxxxG (B) More commits
*   xxxxxxxF Merge branch 'master' into B
|\  
| * xxxxxxxE (master) Another commit on master
| * (more commits here)
| * xxxxxxxD Commit on master
* | xxxxxxxC Another commit 
* | (more commits here)
* | xxxxxxxB First commit on branch A
|/  
* xxxxxxxA (master) some commit
Run Code Online (Sandbox Code Playgroud)

现在,我想重写分支A的历史记录,可能合并或编辑某些提交,但是我也想更改分支A上的第一次提交,并且我想保留合并,以便将master合并到A中。

我首先进行了直观的尝试git rebase -i -p xxxxxxxB,但是很明显,其中不包含xxxxxxxB提交本身。因此,另一尝试git rebase -i -p xxxxxxxB^确实包含了该提交,但现在实际上并没有保留合并。

看起来很有希望的另一种选择是--root,但这是从整个存储库中的第一次提交开始的,这也不是我想要的。

有什么办法可以做我想要的吗?

bar*_*s2x 6

经过比我认为合理的时间之后,我能够在irc上获得解决方案:

git rebase -i -m -r firstCommitInBranch^ 正是我需要的。

从git文档:

   -r, --rebase-merges[=(rebase-cousins|no-rebase-cousins)]
       By default, a rebase will simply drop merge commits from the todo list, and put
       the rebased commits into a single, linear branch. With --rebase-merges, the
       rebase will instead try to preserve the branching structure within the commits
       that are to be rebased, by recreating the merge commits. Any resolved merge
       conflicts or manual amendments in these merge commits will have to be
       resolved/re-applied manually.
       (...)
       The --rebase-merges mode is similar in spirit to --preserve-merges, but in
       contrast to that option works well in interactive rebases: commits can be
       reordered, inserted and dropped at will.
Run Code Online (Sandbox Code Playgroud)

   -m, --merge
       Use merging strategies to rebase. When the recursive (default) merge strategy is
       used, this allows rebase to be aware of renames on the upstream side.
Run Code Online (Sandbox Code Playgroud)

我也错过了文档中说不能-p-i以下部分一起使用的部分:

   -p, --preserve-merges
       Recreate merge commits instead of flattening the history by replaying commits a
       merge commit introduces. Merge conflict resolutions or manual amendments to merge
       commits are not preserved.

       This uses the --interactive machinery internally, but combining it with the
       --interactive option explicitly is generally not a good idea unless you know what
       you are doing (see BUGS below).
Run Code Online (Sandbox Code Playgroud)

  • 注意:`--rebase-merges`是Git 2.18中的新增功能。 (2认同)
  • `preserve-merges` 已从版本 2.34 的 Git 中删除。请参阅[Git 2.34 的亮点](https://github.blog/2021-11-15-highlights-from-git-2-34/)。大多数使用它的尝试都会因致命错误而退出。 (2认同)