Nishaglobal Education logo

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.

Lesson 4 of 1135 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Throw away unsaved changes
  3. Unstage a file
  4. Fix the last commit
  5. Get an old version of a file back
  6. Undo a commit with git revert
  7. Practice
  8. Common mistakes
  9. Recap
  10. Quiz
  11. Mark lesson complete

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…CommandSafe to use after sharing?
Throw away changes I have not stagedgit restore <file>Yes (only affects your folder)
Unstage a file but keep my changesgit restore --staged <file>Yes
Fix the message or content of my last commitgit commit --amendNo, only before you push
Get an old version of one file backgit restore --source=<id> <file>Yes
Undo a whole commitgit 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:

Terminal
printf 'oops\n' > styles.css
git status --short
git restore styles.css
git status --short
cat styles.css

Output

 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

Terminal
printf '<p>Draft text</p>\n' >> index.html
git add index.html
git status --short
git restore --staged index.html
git status --short

Output

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:

Terminal
git commit -q -m "Add contcat details"
git log --oneline -1
git commit --amend -q -m "Add contact details"
git log --oneline -1

Output

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*:

Terminal
git restore --source=HEAD~1 index.html
cat index.html
git status --short

Output

<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:

Terminal
git revert --no-edit HEAD
git log --oneline
cat index.html

Output

[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.

  1. Check with git status --short: index.html shows M.
  2. Run git restore index.html.
  3. Check again and cat the file.
Show the solution
Solution
git status --short
git restore index.html
git status --short
cat index.html

Output

 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
Solution
git revert --no-edit HEAD~1
git log --oneline

Output

[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; --staged unstages but keeps them.
  • git commit --amend replaces 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. 1. You staged a file by mistake but want to keep your edits. Which command?

  2. 2. Why is git revert safe for commits you have already shared?

  3. 3. When should you NOT use git commit --amend?

  4. 4. What happens to uncommitted changes discarded with git restore <file>?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.