as mentioned in the git reset
man page:
$ git branch topic/wip (1)
$ git reset --hard HEAD~3 (2)
$ git checkout topic/wip (3)
- You have made some commits, but realize they were premature to be in the "
master
" branch. You want to continue polishing them in a topic branch, so create "topic/wip
" branch off of the current HEAD
.
- Rewind the
master
branch to get rid of those three commits.
- Switch to "
topic/wip
" branch and keep working.
Note: due to the "destructive" effect of a git reset --hard
command (it does resets the index and working tree. Any changes to tracked files in the working tree since <commit>
are discarded), I would rather go with a:
$ git reset --soft HEAD~3 (2)
, to make sure I'm not loosing any private file (not added to the index).
The --soft
option won't touch the index file nor the working tree at all (but resets the head to <commit>
, just like all modes do).
Note bis, if you hadn't made any commit yet, only (1) and (3) would be enough!
(or, in one command: git checkout -b newBranch
)