代码之家  ›  专栏  ›  技术社区  ›  barteks2x

interactive rebase with--保留当前分支中所有提交的合并

  •  2
  • barteks2x  · 技术社区  · 8 年前

    似乎已经有了一些类似的问题,但没有一个是我想要的。

    假设我有这样的历史

    * 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
    

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

    我第一次直觉地尝试 git rebase -i -p xxxxxxxB ,但显然这不包括xxxxxxb提交本身。所以另一个尝试是 git rebase -i -p xxxxxxxB^ 它确实包含了提交,但现在它实际上并没有保留合并。

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

    有什么办法做我想做的吗?

    1 回复  |  直到 8 年前
        1
  •  6
  •   barteks2x    8 年前

    在比我认为合理的时间更长之后,我就能够在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.
    

       -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.
    

    我还错过了文档中说不使用的部分 -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).
    
    推荐文章