KevsRobots Learning Platform
By , Minute
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.
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 withgit difffirst. 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 ..
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.
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 revertinstead.
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.
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
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.
main.py, then get rid of it with git restore.notes.txt, git add it, then unstage it with git restore --staged.--amend.git revert. Check the log.git revert says there’s a conflictgit 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.
git status says HEAD detached at 3f9c2a1git 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.
git reset --hard and lost a commitgit reflog, find the hash of the lost commit, and run git reset --hard <hash> to go back to it.Quick quiz 1 of 3
You staged secret.txt by mistake. How do you unstage it but keep the file?
--staged only takes the file out of the staging area. Your edits stay.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.
Comments