Recovery with Reset, Stash, and Reflog
What This Concept Is
Git recovery tools let you move work around safely when you make mistakes or need to pause unfinished work.
The three core tools here are:
git reset: move a branch pointer and optionally align index or working treegit stash: shelve unfinished work temporarilygit reflog: inspect where your local references used to point
Why It Matters Here
You do not need to avoid all Git mistakes. You need to avoid panicking when they happen. Recovery is much easier when you know whether you are changing:
- a reference
- the index
- the working tree
That is why reset feels dangerous to beginners. It can touch more than one layer.
Concrete Example
Common safe patterns:
git stash push -m "wip before branch switch"
git switch main
git reset --soft HEAD~1
This undoes the last commit but keeps the changes staged.
git reflog
git reset --hard "HEAD@{1}"
This can recover from a bad local move if the old position still exists in the reflog.
Common Confusion / Misconception
Misconception: reset is one thing.
Correction: reset --soft, default reset, and reset --hard do very different amounts of damage because they touch different layers.
Misconception: Once a branch tip moves, the old commit is gone forever.
Correction: Reflog often gives you a recovery window for local reference movements.
How To Use It
Default safety rule:
- if you are not fully sure, stash or branch before destructive operations
Practical rule of thumb:
stashfor temporary shelvingreset --softfor local commit redo- default
resetfor unstaging reset --hardonly on throwaway or fully understood statereflogwhen you need to find where you were before
Check Yourself
- What is the difference between
reset --softandreset --hard? - Why is
refloglocal rather than shared? - Why is
stashoften the safer first move before risky cleanup?
Mini Drill or Application
In a throwaway repository:
- create two commits
- run
git reset --soft HEAD~1 - inspect
status - recommit
- make more uncommitted edits and stash them
- run
git reflog - deliberately move away from a commit and recover it with the reflog
Write one sentence for each step explaining which layer changed.