Undoing Things and Good Habits
Harry
· 14 Sep 2026
· 2 views
Advertisement
Undo before committing
git restore file.txt # discard unstaged edits to a file
git restore --staged file.txt# unstage (keep the edits)
Fix the last commit
Forgot a file or mistyped the message? Amend it – as long as you have not pushed:
git add forgotten.txt
git commit --amend -m "Add login form and validation"
reset vs revert
These both “undo” commits but differently, and the distinction matters:
git resetmoves the branch pointer back, rewriting history. Safe on commits you have not shared.--softkeeps changes staged,--hardthrows them away.git revertcreates a new commit that undoes an earlier one, keeping history intact. This is the safe choice for commits already pushed to a shared branch.
git reset --soft HEAD~1 # undo last commit, keep changes staged (local only)
git revert a1b2c3d # undo a pushed commit with a new commit
Stash work in progress
Need to switch branches but are not ready to commit? Shelve your changes:
git stash # set unfinished changes aside
git switch main # do the urgent thing
git switch feature/search
git stash pop # bring the changes back
Habits that pay off
- Commit small and often, one logical change per commit.
- Write clear messages in the imperative: “Fix null check in parser”.
- Never rewrite history (
reset, force-push) on branches others use. - Pull before you start and before you push to reduce conflicts.
Key points
restoreundoes uncommitted edits;commit --amendfixes the last (unpushed) commit.resetrewrites local history;revertsafely undoes shared commits.stashshelves work in progress so you can switch context.- Small commits, clear messages, and never rewriting shared history keep a repo healthy.