Move the most recent commit(s) to a new branch with Git

前端 未结 14 1242
梦毁少年i
梦毁少年i 2020-11-22 03:28

I\'d like to move the last several commits I\'ve committed to master to a new branch and take master back to before those commits were made. Unfortunately, my Git-fu is not

14条回答
  •  太阳男子
    2020-11-22 03:58

    Had just this situation:

    Branch one: A B C D E F     J   L M  
                           \ (Merge)
    Branch two:             G I   K     N
    

    I performed:

    git branch newbranch 
    git reset --hard HEAD~8 
    git checkout newbranch
    

    I expected that commit I would be the HEAD, but commit L is it now...

    To be sure to land on the right spot in the history its easier to work with the hash of the commit

    git branch newbranch 
    git reset --hard #########
    git checkout newbranch
    

提交回复
热议问题