【问题标题】:Git/source tree move specific commits at branch B to branch AGit/sourcetree 将分支 B 中的特定提交移动到分支 A
【发布时间】:2017-06-07 12:29:28
【问题描述】:

我在开发时发生了意外。我有两个提交需要在分支 A,但我推送到分支 B。所以现在,我想将这些提交移动到分支 A,然后从分支 B 中删除它们。请查看图片了解详细信息:

【问题讨论】:

    标签: git github terminal sourcetree


    【解决方案1】:

    首先,转到branchAcherry-pick 您要在此处选择的两个提交。

    $ git checkout branchA
    $ git cherry-pick <commit1>       # commit1 = 0a18e0f   
    $ git cherry-pick <commit2>       # commit2 = e604ce4
    
    $ git push origin HEAD            # push to remote
    

    现在从branchB 中删除revertrebase 中的两个提交。 Revert 更可取,因为它不会更改 git 历史记录。

    还原:

    $ git checkout branchB
    $ git revert <commit1>
    $ git revert <commit2>
    $ git push origin HEAD
    

    变基:

    $ git checkout branchB
    $ git rebase -i efb2443         # go back to the commit before the two commmits you want to remove
    
    Now comment out (adding `#` before the commit hash) the two commits you want to remove.
    
    $ git push -f origin HEAD       # you need to force(-f) push as history is changed here by rebasing
    

    【讨论】:

    • 在cherry-pick(将代码推送到服务器)之后我还需要做其他事情吗?
    • @lee 最好还原分支 B 上的这两个提交,而不是使用交互式 rebase (rebase -i) 删除它们。如果有人(或许多人)拉取了您之前推送到分支 B 的更改,那么当您(强制)推送更新的分支时,您将为他们重写历史记录。这会让他们头疼,而且是违反 git 礼仪的。
    • 非常感谢您的帮助。
    猜你喜欢
    • 2016-03-07
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2014-11-19
    相关资源
    最近更新 更多