History and undo
Go back when something breaks
Saving is only half the story. The real power of Git is going back. This lesson shows you how to read your history and undo changes safely.
What is history?
Think of your phone's photo gallery. You scroll back and see every photo in order, each with a date. Git history is a gallery of your commits.
- Each commit has a short ID, like
a1b2c3d - Each commit has your message
- The newest commit is at the top
Viewing your history
git log --oneline9f3e21a Fix streak count resetting at midnight
4c7b0d2 Add habit list to home page
a1b2c3d Add README with project titleLet's break this down:
git logshows your commits--onelineshows one short line per commit, which is easier to read- The first column is the commit ID. You can use it to point at a commit
If the list is long, press q to get back to the terminal.
Undo changes you haven't committed
Say the AI edited index.html and now the page is blank. You haven't committed yet. You can throw away those edits and go back to the last save point:
git restore index.htmlTo throw away every uncommitted change in the folder:
git restore .git restore cannot be undone. The unsaved edits are gone for good. Run git status first so you know what you are throwing away.Going back to an older commit
If the broken change is already committed, the safest move is git revert. It adds a new commit that undoes an old one. Your history stays complete.
git revert 9f3e21aYou don't need to memorize this. You can ask an AI tool like Claude Code to do it:
My app worked before the last commit. Now the habit list is empty.
Show me the last 5 commits, explain what the latest one changed,
and safely undo it without deleting any history.Commit before big AI changes
This is the most important habit in this course. Before you ask AI for a big change, make a commit. That is your save point before the boss fight.
git add .
git commit -m "Working version before adding charts"Now ask the AI for the change. If the result is a mess, you can restore everything in one step.
Branches, briefly
A branch is a separate copy of your project's timeline. It is like a "what if" save slot in a game. You can try a risky idea there without touching your main version.
git switch -c try-dark-modeThis creates and moves to a new branch called try-dark-mode. Your main branch is usually called main. For now, you can work on main and use commits as your safety net.
Common mistakes
- Wrong: asking AI to "rebuild the whole app" with no commit first. Right: commit, then ask.
- Wrong: running
git restore .without checking. Right: rungit statusfirst. - Wrong: deleting files by hand to "go back". Right: let Git restore or revert them.
Practice
Try these exercises to lock in what you just learned.
What's next?
Your history lives only on your computer right now. Next, let's put it in the cloud with GitHub.