git: how to move some commits to new branch

前端 未结 1 1846
执念已碎
执念已碎 2020-12-02 09:07

I have been working in straight line:

A---B---C---D---E---F (master:HEAD)

Now I want to move backward:

git checkout C


        
相关标签:
1条回答
  • 2020-12-02 09:40

    To get from your first diagram (master = HEAD = F) to option 1:

    git branch new        # Make a 'new' branch pointing at HEAD, which is F
    git reset --hard C    # Move master back to point at C
    git checkout new      # Make HEAD follow new, and get F in the working tree
    

    And from option 1 to option 2 (picking up where the above left off),

    git rebase -i master  # Start the process of re-jiggering this branch
    # edit the commit list that opens up to only include F
    # save and exit
    # resolve potential conflicts from missing changes in D and E
    

    To go directly from your starting point to option 2:

    git checkout -b new C  # Start the 'new' branch at C
    git cherry-pick F      # Include F on it
    git checkout master    # Switch back to master
    git reset --hard C     # Rewind master to the earlier commit
    
    0 讨论(0)
提交回复
热议问题