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

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

    For those wondering why it works (as I was at first):

    You want to go back to C, and move D and E to the new branch. Here's what it looks like at first:

    A-B-C-D-E (HEAD)
            ↑
          master
    

    After git branch newBranch:

        newBranch
            ↓
    A-B-C-D-E (HEAD)
            ↑
          master
    

    After git reset --hard HEAD~2:

        newBranch
            ↓
    A-B-C-D-E (HEAD)
        ↑
      master
    

    Since a branch is just a pointer, master pointed to the last commit. When you made newBranch, you simply made a new pointer to the last commit. Then using git reset you moved the master pointer back two commits. But since you didn't move newBranch, it still points to the commit it originally did.

提交回复
热议问题