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
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.