For those curious about why it works.
You want to reset to commit C and move commits D and E to a new branch.
Initially, it looks like this:
shellA-B-C-D-E (HEAD) ↑ master
After git branch newBranch:
shellnewBranch ↓ A-B-C-D-E (HEAD) ↑ master
After git reset --hard HEAD~2:
shellnewBranch ↓ A-B-C-D-E (HEAD) ↑ master
Since branches are simply pointers, master points to the latest commit. When you create newBranch, you simply create a new pointer pointing to the latest commit. Then using git reset, you move the master pointer back two commits. But since you didn't move newBranch, it still points to the commit it was initially created on.
2024年6月29日 12:07 回复