Lessons · Git · soft, mixed and hard
Soft, mixed and hard
Three depths in order. Soft moves the branch. Mixed moves the branch and the index. Hard moves those and your files.
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
Learning them as a sequence rather than three unrelated flags is what makes reset predictable instead of frightening.
How to think about it
Say what should happen to the changes before you type the flag, then pick the depth that matches. Only the deepest one can lose work.
Worked example
git reset --soft HEAD~1Depth one: the branch only.
git status --shortStaged. The index still holds what the commit held.
git reset HEAD~0Mixed is the default, and here it unstages.
git status --shortThe same change, one column to the right.
git add f.txtPut it back.
git commit -qm twoAnd recommit, so the next line has something to undo.
git reset --hard HEAD~1Depth three. The file on disk goes back too.
cat f.txtone. Whatever was in the working tree is gone.
Your turn
Move the branch back one and discard the changes entirely.
git reset HEAD~1
Try it in a real repository
The trap
Using --hard because it is the one you remember. Soft and mixed cannot lose an edit; hard is the only one that overwrites files.
Practise soft, mixed and hard on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.