Lessons · Git · moving the branch back
Moving the branch back
git reset HEAD~1 moves the branch back one commit. --soft keeps the changes staged, the default keeps them in the working tree, --hard discards them too.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Redo a commit, split one into two, drop an experiment: reset is the tool, and the flag decides whether the work survives.
How to think about it
Say what should happen to the changes before you type the flag. Keep them staged: --soft. Keep them as edits: no flag. Throw them away: --hard, and only when you are sure.
Worked example
git reset --soft HEAD~1Last commit gone; its changes stay staged.
git status --shortA or M in the first column: ready to recommit.
git reset HEAD~1Mixed, the default: the commit goes, the changes stay in the working tree, unstaged.
git reset --hard HEAD~1Commit gone, changes gone from the files too. No undo for uncommitted work.
Your turn
Take back the last commit but keep everything staged.
git reset HEAD~1
Try it in a real repository
The trap
--hard throws away uncommitted work permanently. Committed work can be found again: git reflog is a record of everywhere HEAD has been for the last few weeks, so a commit you moved off is still reachable by its hash. Uncommitted work was never written down anywhere, so there is nothing to find: it is gone for good.