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

Git:处理合并后的分支,该分支后来未合并

  •  4
  • MJohnyJ  · 技术社区  · 7 年前

    我有以下问题: 分支A被合并到主分支,到目前为止还不错。

    一段时间后,客户决定撤消在主分支上的分支A中完成的更改,因为它必须更新。

    Some stuff
    Some other stuff
    Some stuff, where the merge of Branch A was reverted
    Some other stuff
    Some other stuff
    Branch A was merged into master
    The commit of Branch A (Branch A contains one Commit)
    

    我想重新设置分支A的基址,所以我想要如下内容:

    The commit of Branch A (From here I want to continue working)
    Some stuff
    Some other stuff
    Some stuff, where the merge of Branch A was reverted
    Some other stuff
    Some other stuff
    Branch A was merged into master
    The commit of Branch A (Branch A contains one Commit)
    

    注意,我不想更改远程主机的历史记录,我需要将分支A的提交作为主机最后一次提交的基础。然后我想再次将更新后的分支A推给Master。

    我不想把我在分支A中更改的文件一个接一个地复制到另一个分支上,这是从master签出的。我认为有更好的解决方案,有什么建议吗?

    3 回复  |  直到 7 年前
        1
  •  1
  •   Code-Apprentice    7 年前

    如果您使用 git checkout branch-a && git rebase -i master ,您将在文本编辑器中使用 pick 命令在每个前面。您可以将其更改为几个选项之一,所有这些选项都在给定的文本中进行了解释。例如,您可以 skip 恢复提交,这些更改将不会恢复。

    另一个潜在的解决方案是重新调整现有的基础 git revert branch-a

        2
  •  1
  •   hspandher    7 年前

    HEAD 你的 BranchA master ,git认为master中的代码比 . 在这种情况下,如果您重置了主分支中的更改,而不是使用显式提交来恢复它们,那会更好。

    以下所有选项都涉及重写历史,因此请首先在一些临时分支中尝试它们。

    一种选择是使用 rebase --onto 这样,合并和还原提交都将被删除。

    git rebase --onto <commit_id_before_merge_commit> <revert_merge_commit_id> master
    

    这样git就不会像master那样识别它们。

    git checkout BranchA
    git reset HEAD^n # n is the number of commits that are exclusive to branchA
    git add -u # Add all the files. Make sure to add newly created ones.
    git commit -m 'some message'
    git rebase master # should work now
    

    第三,您还可以使用交互式重定基来更改

    git rebase -i <first_exclusive_initial_commit_id_of_branchA>
    
        3
  •  1
  •   cmaster - reinstate monica    7 年前

    由于您不想接触master的历史记录,因此需要将您的更改重新应用到master的顶部,忽略它们已经应用并恢复的事实。根据还原提交的确切性质,我们将其称为 <revert>

    1. <还原(>); 只有 取消您的更改,然后您可以再次还原它以获得重新应用更改的提交(想想 --x == x

    2. 如果恢复提交不是纯粹的,我会这样处理这种情况:

      • git format-patch .

      • 使用将此修补程序序列应用于master git am

      吉特am