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

前端 未结 14 1504
梦毁少年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:53

    1) Create a new branch, which moves all your changes to new_branch.

    git checkout -b new_branch
    

    2) Then go back to old branch.

    git checkout master
    

    3) Do git rebase

    git rebase -i 
    

    4) Then the opened editor contains last 3 commit information.

    ...
    pick  C
    pick  D
    pick  E
    ...
    

    5) Change pick to drop in all those 3 commits. Then save and close the editor.

    ...
    drop  C
    drop  D
    drop  E
    ...
    

    6) Now last 3 commits are removed from current branch (master). Now push the branch forcefully, with + sign before branch name.

    git push origin +master
    

提交回复
热议问题