Undoing changes
Everyone makes mistakes: a typo in a commit message, a file committed by accident, a change that broke everything. Git can undo almost anything, if you pick the right tool. This lesson is your rescue guide.
Quick reference: "I want to…"
| Situation | Command | Safe on shared branches? |
|---|---|---|
| Discard uncommitted edits to a file | git restore <file> |
✅ (local only) |
| Unstage a file (keep the edits) | git restore --staged <file> |
✅ |
| Fix the last commit's message or add a forgotten file | git commit --amend |
⚠️ Only if not pushed yet |
| Undo the last commit, keep its changes staged | git reset --soft HEAD~1 |
⚠️ Only if not pushed yet |
| Undo the last commit, keep its changes unstaged | git reset HEAD~1 |
⚠️ Only if not pushed yet |
| Throw away the last commit and its changes | git reset --hard HEAD~1 |
⚠️ Destructive |
| Undo a commit that's already pushed | git revert <commit> |
✅ The safe way |
| Shelve work temporarily | git stash / git stash pop |
✅ |
| Recover "lost" commits | git reflog |
✅ |
HEAD~1 means "one commit before HEAD", and HEAD~3 means three commits back.
Discarding and unstaging
⚠️
git restore <file>permanently discards uncommitted edits. There's no undo for work that was never committed.
git reset: move the branch backwards
reset moves your branch pointer to an earlier commit. The mode decides what happens to the changes from the commits you "removed":
--soft → changes stay STAGED
(default) --mixed → changes stay in your files, UNSTAGED
--hard → changes are DELETEDTry it again with git reset --hard HEAD~1 and notice that c.txt is gone (ls).
git revert: undo safely on shared history
If a commit is already pushed and shared, never rewrite history. Teammates would end up with conflicting histories. Instead, revert: Git creates a new commit that does the opposite.
The bad commit stays in history, but its effect is cancelled, and about.html from the later commit is untouched.
Amending the last commit
git commit -m "Add lgoin page" # typo!
git commit --amend -m "Add login page" # replace the message
git add forgotten-file.js
git commit --amend --no-edit # add a file to the last commit, keep the message--amend replaces the last commit with a new one. Use it only on commits you haven't pushed.
The safety net: git reflog
Git records every place HEAD has been in the reflog, including commits you "deleted" with reset --hard. Real Git keeps these for about 90 days.
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~1
# 9f8e7d6 HEAD@{1}: commit: The commit I thought I lost
git reset --hard 9f8e7d6 # it's back!
# or: git switch -c rescued 9f8e7d6The rule: if it was ever committed, it can almost always be recovered. That's another reason to commit often.
Removing a file from Git but keeping it on disk
Committed .env or node_modules by mistake?
echo ".env" >> .gitignore
git rm --cached .env # stop tracking, keep the local file
git commit -m "Stop tracking .env"(If it contained secrets, rotate them. They still exist in the history.)
Practice
- Make 4 commits. Use
git reset --soft HEAD~2to combine the last two into one new commit with a better message. - Make a "bad" commit, then a good one after it, and use
git revertto undo only the bad one. - Explain to yourself why
revertis safe for shared branches andresetisn't.
1.A bad commit was pushed to main and teammates have pulled it. How do you undo it?
2.What does git reset --soft HEAD~1 do?