Git & GitHub・Your first commit

Undoing mistakes

Getting a file back, undoing an add, fixing a typo in a commit message — how you undo in Git depends on the situation. Here are the four you'll actually use, ranked by how dangerous they are.

Being able to go back is the whole reason to use Git. But how you go back depends on the situation. Here are the four you’ll actually use, with a danger rating.

Which one to use

What you wantCommandDanger
Throw away edits and return to the last commitgit restore filenameHigh (edits are gone)
Undo a git addgit restore --staged filenameLow
Fix the message on your last commitgit commit --amendMedium
Cancel out an earlier commitgit revert commit-idLow

Only the first one truly destroys anything. Let’s take them in order.

1. Throw away edits: git restore

For “I tried a bunch of things and I want none of it.”

git restore style.css

style.css returns to its state at the last commit. To undo everything in the folder:

git restore .

2. Undo an add: git restore —staged

For “I put it in the box, but it doesn’t belong in this record.”

git restore --staged script.js

With --staged, it’s only taken out of the box — your edits are untouched. Safe to use freely.

Helpfully, git status tells you about both:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)   ← take out of the box

Changes not staged for commit:
  (use "git restore <file>..." to discard changes)    ← throw away edits

Git shows you the commands available right now. “When in doubt, git status” applies here too.

3. Fix your last commit: git commit —amend

For when you notice a typo in the message, or a forgotten file, right after committing.

git commit --amend -m "Add the favorites list"

Your last commit is overwritten with the new message. If you forgot a file, git add it first and run the same command to fold it into that same commit.

4. Cancel an earlier commit: git revert

For “the change three commits back is what broke it.” First find the commit ID:

git log --oneline
c3d4e5f Change the send button color
b2c3d4e Add the form behavior
a1b2c3d Build the page skeleton

Then name the commit you want to cancel:

git revert b2c3d4e

Git stacks on a new commit that undoes that one’s changes. The history isn’t erased, and the fact that you undid it is recorded too — which is exactly why it’s safe.

You can skip git reset for now

Search around and you’ll meet git reset --hard. It’s a powerful command that rewinds the history itself, and pointing it at the wrong place erases work in bulk.

As a beginner, the four above cover nearly everything. Just know that the stronger tool exists, and look it up when you truly need it.

Summary of this lesson

  1. git restore throws away edits — it can’t be undone, so check the target first
  2. git restore --staged undoes an add. Your edits survive, so it’s safe
  3. Fix the last commit with --amend (before pushing); cancel a pushed commit with git revert

FAQ

How do I discard my edits and go back to the last commit?
For changes you haven't committed, git restore filename returns the file to its state at the last commit. The edits are gone for good, so there's no undoing this undo.
How do I undo a git add?
git restore --staged filename. It takes the file out of the box for the next record without touching your edits.
What's the difference between git reset and git revert?
reset rewinds the history itself; revert adds a new commit that cancels out an old one. For commits you've already pushed to GitHub, revert is the safe choice.