Undoing changes safely
Fix mistakes with confidence in Git: throw away unsaved edits with git restore, unstage files, amend the last commit, restore an old version of a file, and undo a shared commit with git revert.
🎯 By the end of this lesson you will be able to:
- Discard working-folder changes and unstage files with git restore
- Fix the last commit with git commit --amend
- Undo a commit safely with git revert
Outline
What you'll learn and why it matters
The main reason to use Git is that mistakes become easy to fix. Deleted the wrong paragraph? Committed a typo? Broke the page with yesterday's change? There is a safe command for each. Knowing them takes away the fear of experimenting.
| I want to… | Command | Safe to use after sharing? |
|---|---|---|
| Throw away changes I have not staged | git restore <file> | Yes (only affects your folder) |
| Unstage a file but keep my changes | git restore --staged <file> | Yes |
| Fix the message or content of my last commit | git commit --amend | No, only before you push |
| Get an old version of one file back | git restore --source=<id> <file> | Yes |
| Undo a whole commit | git revert <id> | Yes: it adds a new commit |
Throw away unsaved changes
Asha accidentally replaced her whole stylesheet. Nothing is staged, so she can get the last committed version back:
printf 'oops\n' > styles.css
git status --short
git restore styles.css
git status --short
cat styles.cssOutput
M styles.css
h1 { color: navy; }git status --short is a compact status: M means modified but not staged. After git restore, there are no changes and the file is back to its committed content.
Unstage a file
printf '<p>Draft text</p>\n' >> index.html
git add index.html
git status --short
git restore --staged index.html
git status --shortOutput
M index.html M index.html
M (letter in the first column) means staged; M (letter in the second column) means changed but not staged. --staged only takes the change out of the staging area. The edit is still in the file.
Fix the last commit
Spotted a typo in the message, or forgot a file? --amend replaces the last commit with a corrected one:
git commit -q -m "Add contcat details"
git log --oneline -1
git commit --amend -q -m "Add contact details"
git log --oneline -1Output
aabe51d (HEAD -> main) Add contcat details a3d0d8d (HEAD -> main) Add contact details
Notice the id changed: an amended commit is really a new commit that replaces the old one. That is why you must only amend commits you have not pushed to GitHub yet. Rewriting shared history causes problems for everyone else.
Get an old version of a file back
--source tells git restore which commit to take the file from. HEAD~1 means *one commit before the current one*:
git restore --source=HEAD~1 index.html
cat index.html
git status --shortOutput
<h1>Asha Kumar</h1> <p>Junior web developer</p> M index.html
The file is now as it was before the intro sentence was added, shown as a normal change you can commit, or undo again with git restore index.html.
Undo a commit with git revert
When a bad commit has already been shared, do not delete it. git revert creates a new commit that does the exact opposite, so history stays honest and nobody's copy breaks:
git revert --no-edit HEAD
git log --oneline
cat index.htmlOutput
[main 6e4dfb1] Revert "Add intro sentence" 1 file changed, 1 deletion(-) 6e4dfb1 (HEAD -> main) Revert "Add intro sentence" 1ce7ed1 Add intro sentence ec605b9 Add stylesheet 6103896 Add home page <h1>Asha Kumar</h1> <p>Junior web developer</p>
Without --no-edit, Git opens your editor with a ready-made message (*Revert "…"*); save and close it to finish.
Practice
Guided exercise
Rescue a broken page
Asha deleted everything in index.html by mistake and has not staged it. Restore the file, then show the short status and the file's contents.
- Check with
git status --short: index.html showsM. - Run
git restore index.html. - Check again and
catthe file.
Show the solution
git status --short
git restore index.html
git status --short
cat index.htmlOutput
M index.html <h1>Asha Kumar</h1> <p>Junior web developer</p> <p>I build accessible websites.</p>
How to get there: The first status shows the file modified. After restoring, status prints nothing (no changes) and the page's three lines are back. This only works because the content was committed earlier: commit often.
Your turn
Undo the stylesheet commit
The Add stylesheet commit turned out to be a mistake and has been shared. Undo it safely, without opening an editor, and show the one-line history.
Show the solution
git revert --no-edit HEAD~1
git log --onelineOutput
[main be6c154] Revert "Add stylesheet" 1 file changed, 1 deletion(-) delete mode 100644 styles.css be6c154 (HEAD -> main) Revert "Add stylesheet" 1ce7ed1 Add intro sentence ec605b9 Add stylesheet 6103896 Add home page
How to get there: HEAD~1 is the stylesheet commit (one before the latest). Revert adds a new commit that removes styles.css, keeping all history. You could also use the commit's short id from git log --oneline.
Common mistakes
Using git restore when you meant git restore --staged
You see: Your edits are gone instead of just unstaged
Fix: Read the command twice; --staged keeps your changes.
Amending a commit you have already pushed
You see: Your push is rejected, or teammates get confusing conflicts
Fix: Only amend local commits; use git revert for shared ones.
Using git reset --hard to undo
You see: Lost work
Fix: Prefer git restore for files and git revert for commits.
Forgetting --no-edit and getting stuck in an editor
You see: A text editor opens during revert or amend
Fix: Save and close the editor (in Vim: type :wq and Enter).
Recap
git restore <file>discards unstaged changes;--stagedunstages but keeps them.git commit --amendreplaces the last commit: only before pushing.git restore --source=<id> <file>brings back an old version of a file.git revert <id>undoes a commit with a new commit: safe for shared history.
Key terms
- Amend
- Replace the most recent commit with a corrected one.
- Revert
- Create a new commit that undoes the changes of an earlier commit.
- HEAD~1
- The commit one step before the current one.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. You staged a file by mistake but want to keep your edits. Which command?
2. Why is git revert safe for commits you have already shared?
3. When should you NOT use git commit --amend?
4. What happens to uncommitted changes discarded with git restore <file>?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.