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

前端 未结 14 1395
梦毁少年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 04:08

    Yet another way to do this, using just 2 commands. Also keeps your current working tree intact.

    git checkout -b newbranch # switch to a new branch
    git branch -f master HEAD~3 # make master point to some older commit
    

    Old version - before I learned about git branch -f

    git checkout -b newbranch # switch to a new branch
    git push . +HEAD~3:master # make master point to some older commit 
    

    Being able to push to . is a nice trick to know.

提交回复
热议问题