KevsRobots Learning Platform

Git for Beginners

% complete

Reset my progress

Undoing Changes

Throw away edits with git restore, unstage files, fix your last commit with --amend, and undo commits safely with git revert

By ,    Minute


Mistakes are fine

The whole point of Git is that mistakes stop being scary. Which command you need depends on where the mistake is:

Where’s the mistake? What you want Command
Edited a file, not staged Throw the edit away git restore <file>
Staged something by accident Unstage it, keep the edit git restore --staged <file>
Last commit has a typo or missed a file Fix the last commit git commit --amend
A commit broke something Undo it with a new commit git revert <hash>

Let’s try each one on pico-bot.


Throw away an edit: git restore

Say you’ve been experimenting and made a mess of main.py. It isn’t staged, and you just want it back the way it was at the last commit:

git restore main.py

The file instantly goes back to how it was in the last commit.

This one is permanent

git restore <file> throws your uncommitted edits away, and Git can’t get them back because they were never committed. Check with git diff first. This is one of the very few ways to really lose work in Git.

To throw away all uncommitted edits in the whole project, use git restore ..


Unstage a file: git restore –staged

You ran git add . and accidentally staged notes.txt along with main.py. To take it out of the staging area but keep your edits:

git restore --staged notes.txt

Now git status shows notes.txt as not staged, and main.py is still staged and ready to commit. This one is completely safe: nothing is lost.


Fix the last commit: git commit –amend

You’ve just committed, then spotted a typo in the message, or realised you forgot to add a file. Amend lets you redo the last commit.

To change just the message:

git commit --amend -m "Set up the right motor pin"

To add a forgotten file to the last commit:

git add README.md
git commit --amend --no-edit

--no-edit keeps the existing message.

Only amend commits you haven’t shared

Amending replaces the last commit with a new one (it gets a new hash). That’s fine on your own computer, but if you’ve already pushed the commit to GitHub (module 10), other people may have the old one. For shared commits, use git revert instead.


Undo a commit safely: git revert

Suppose a commit two days ago made the robot drive backwards. You don’t want to rewrite history; you just want to undo that change. git revert makes a new commit that does the exact opposite of the old one:

git log --oneline
c7d2e19 (HEAD -> main) Add README
a41f8b3 Reverse motor direction
5d0e7b2 Set up the right motor pin
8b1e4d0 Set up the left motor pin
3f9c2a1 Add start-up code that blinks the LED
git revert a41f8b3

Git opens your editor with a message like Revert "Reverse motor direction". Save and close it, and look at the log again:

e5a9c40 (HEAD -> main) Revert "Reverse motor direction"
c7d2e19 Add README
a41f8b3 Reverse motor direction
...

The bad change is undone, and the history still shows what happened and when. This is the safe way to undo, especially on shared branches.


Going back in time to look around

Sometimes you just want to see your project as it was, for example to check whether an old version of the robot code worked. You can check out any commit:

git switch --detach 3f9c2a1

Your files now look exactly as they did in that commit. This is called a detached HEAD state, which simply means you’re looking at a commit rather than a branch (git status will say HEAD detached at 3f9c2a1). Have a look around, then go back to the present with:

git switch main

A word about git reset

You’ll see git reset a lot online. It moves your branch back to an earlier commit. The gentlest version undoes the last commit but keeps all its changes staged, so nothing is lost:

git reset --soft HEAD~1

HEAD~1 means “one commit before HEAD”. Be careful with git reset --hard, which throws away commits and uncommitted changes. As a beginner, reach for git revert first.


Try it yourself

  1. Scribble some nonsense into main.py, then get rid of it with git restore.
  2. Create notes.txt, git add it, then unstage it with git restore --staged.
  3. Make a commit with a deliberate typo in the message, then fix it with --amend.
  4. Make a commit that changes the LED blink to 10 times, then undo it with git revert. Check the log.

Common issues

  • Problem: git revert says there’s a conflict
  • Solution: Later commits changed the same lines. Fix the file as shown in module 9, then run git add and git revert --continue. Or give up with git revert --abort.
  • Why: Git can’t automatically undo a change if the lines have changed again since.

  • Problem: git status says HEAD detached at 3f9c2a1
  • Solution: Run git switch main to get back to your branch.
  • Why: You checked out a commit directly rather than a branch. Commits you make here don’t belong to any branch, so don’t work here.

  • Problem: You used git reset --hard and lost a commit
  • Solution: Run git reflog, find the hash of the lost commit, and run git reset --hard <hash> to go back to it.
  • Why: The reflog remembers everywhere HEAD has been recently, so lost commits can usually be rescued. Uncommitted changes can’t.

Check your understanding

Quick quiz 1 of 3

You staged secret.txt by mistake. How do you unstage it but keep the file?

Quick quiz 2 of 3

A commit you pushed to GitHub last week broke the robot. What’s the safest way to undo it?

Quick quiz 3 of 3

Which of these can permanently lose work?


You can use the arrows  ← → on your keyboard to navigate between lessons.


What are you looking for?
Watch Videos Get Ideas Learn Something Read a Review Read the Blog Search
... Z Z z