Git rebase 而不更改历史记录

问题描述 投票:0回答:1

我有两个分行:

Branch1:  (old) A B C D E F G H (new)
Branch2:  (old) A B   D E F     (new)

注意,C 从分支 2 中删除,然后 G 和 H 被添加到分支 1

我想在 Branch2 上对 Branch1 进行变基,但我不希望 C 重新出现,因为它已从 Branch2 中删除。我只想快进从 Branch2 推送到 Branch1。重申一下,C 必须从历史中消失。

我的失败方法:

git checkout Branch1
git rebase Branch2

Branch1:  (old) A B D E F C G H (new)

什么不起作用:

  • 我没有确切的提交,我正在自动化整个事情,所以我不能使用
    • git rebase --onto Branch2 F G
    • git cherry-pick F G
  • 我只有两个分支名称,仅此而已。

我主要是在寻找某种方法来告诉 git rebase 仅选择快进提交。可以吗?

git git-rebase
1个回答
0
投票

一种方法是以某种方式跟踪

Branch1
历史记录中已审查并集成或丢弃的点,然后用它来运行
git rebase --onto Branch2 <that_point> Branch1


一个例子可以是保留对该提交的引用,例如

  • 一个分支:
    git branch integration/Branch1 <F>
    (<- meaning "commit F on Branch1")
  • 或您为其选择名称的参考:
    git update-ref --create-reflog -m "<some message for your reflog>" \
           refs/integration/Branch1 <F>
    

当您在 Branch2 上集成新提交时,您应该更新该引用。 假设您在 Branch2 上包含提交

G

  • git branch -f integration/Branch1 G
  • 或者:
    git update-ref --create-reflog -m "<some message for your reflog>" \
           refs/integration/Branch1 <G>
    

当您想要重新设置 Branch1 的基础(或至少尝试这样做)时,您可以使用该名称:

git rebase --onto Branch2 integration/Branch1 Branch1

如果成功,您应该更新“集成”参考:

*

git branch -f integration/Branch1 Branch2

*

git update-ref --create-reflog -m "<some message for your reflog>" \
    refs/integration/Branch1 Branch2
© www.soinside.com 2019 - 2024. All rights reserved.