Lesson 8 of 10 · Own the code AI writes
Git for beginners your undo button for AI changes
Git for beginners who build with AI: the handful of commands you need, the habit that makes AI changes safe, and how to undo almost anything.
If you take one habit from this course, take this one: save a snapshot of your project before you let an AI change it. Then whatever it does, you can see exactly what changed, keep it, or throw it all away and be back where you were in seconds.
That's what Git does. It's the tool every developer uses to track changes to code, and it's free. It sounds technical. You only need a handful of commands. This lesson covers them.
What's the difference between Git and GitHub?
Git runs on your computer. It keeps the history of your project in a hidden folder called .git, inside the project.
GitHub is a website where you can keep a copy of that history online, as a backup and to share it. GitLab and Bitbucket do the same job. You can use Git without GitHub, but you want both: Git for the undo button, GitHub so your laptop isn't the only copy.
The words you need
- Repository, or repo: a project that Git is tracking.
- Commit: a snapshot of the project at a moment, with a short message saying what changed.
- Diff: the difference between two versions, line by line. Lesson 9 is all about reading them.
- Branch: a separate line of work, so you can try something without touching the version that works.
Set it up once
Open a terminal (in VS Code: Terminal, then New Terminal) and check Git is installed:
git --version
If you get a version number, you're set. If not, install it from git-scm.com. Then tell Git your name and email, once per computer, so your commits are labelled:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Start tracking a project
In your project's folder, if it isn't a repo already:
git init -b main
-b main calls the main line of work main, which is what GitHub expects. Without it, some versions of Git call it master.
Before your first commit, check .gitignore. It's the list of files Git should never save. It must cover .env and .env.local (your secrets, from lesson 6) and node_modules (thousands of files you can download again). Projects made with create-next-app come with one that has a single line, .env*, covering every file whose name starts .env. That includes .env.example, which you do want saved, so add a line !.env.example under it.
Then:
git status
git status is the command you'll type most. It tells you what's changed since your last commit. Run it whenever you're not sure where you are.
Now save your first snapshot:
git add .
git commit -m "Working sign-up form, before any changes"
git add . picks every changed file to go in the next commit. git commit saves the snapshot with your message. Write the message for yourself in six months: what works now, or what you changed and why.
The habit: commit, ask, look, decide
This is the loop that makes building with AI safe:
- Commit what works.
- Ask the AI for one change.
- Look at what it changed:
git statuslists the files,git diffshows every changed line. New files don't show ingit diff:git statuslists them under "Untracked files", so open and read each one. VS Code's Source Control panel shows both. - Decide. If it's good and it works, commit it. If not, throw it away.
Throwing it away is one command:
git restore --staged --worktree .
That puts every file Git was already tracking back to how it was at your last commit, including changes that were already picked with git add (some AI tools do that for you). New files the AI created aren't touched: git status lists them under "Untracked files". Delete the ones you don't want.
Be careful with this command: it throws away every uncommitted change, yours as well as the AI's, and there's no undo for that. Which is why the habit starts with a commit.
How to undo a commit in Git
Look at your history first:
git log --oneline
Each line is a commit, newest first, starting with a short ID like a1b2c3d. To undo one commit:
git revert --no-edit a1b2c3d
git revert doesn't delete anything. It makes a new commit that does the opposite of the old one, so your history stays honest and nothing is lost. --no-edit uses a ready-made message; without it, Git opens a text editor for one, which can be hard to get out of if you've never seen it.
You'll see AI tools and old answers online suggest git reset --hard. It moves the project back and deletes everything after that point, including work you haven't committed. Don't run it until you understand it, and don't let an AI run it for you.
Try ideas on a branch
When you want the AI to try something bigger, give it a branch:
git switch -c try-new-checkout
That makes a new branch and moves you onto it. Commit as you go. If it works, go back to your main branch and bring the work in:
git switch main
git merge try-new-checkout
If it doesn't, go back to main and the experiment stays out of the way. In older projects the main branch may be called master; git branch lists the branches and marks the one you're on.
Checkpoints in AI tools aren't enough
Some AI tools have their own undo. Claude Code saves checkpoints as it edits, and you can go back with /rewind or by pressing Esc twice. It's handy for "undo that last change".
Its own docs say where it stops, though. A file changed only by a command Claude ran, rather than by its editing tools, isn't tracked: "To revert it, use version control, such as Git." And checkpoints are for "quick, session-level recovery. For permanent version history and collaboration, continue using version control, such as Git". (Claude Code's docs on checkpointing.)
Use the tool's undo for the last five minutes. Use Git for everything else.
Keep a copy on GitHub
Create a free account on GitHub. The easiest way to connect your project is from VS Code: in the Source Control panel, "Publish Branch" signs you in to GitHub and creates the repository for you. Choose private.
If you'd rather use the terminal, sign in first: install GitHub's own command-line tool and run gh auth login, because GitHub won't accept your account password from Git. Then make a new private repository on GitHub, and it shows you the commands to connect your project to it. They look like this:
git remote add origin https://github.com/your-name/your-project.git
git branch -M main
git push -u origin main
After that, git push sends your new commits up. Do it at the end of every working session.
If your app lives in an AI app builder, look for its GitHub or export setting. Most builders can connect to GitHub or let you download the code. Use it, so the only copy of your app isn't inside someone else's website.
If you built something with AI
What to check
- Your project is a Git repo:
git statusin its folder doesn't say "not a git repository". .gitignorecovers.env,.env.localandnode_modules, andgit log --all --oneline -- .env .env.local .env.productionprints nothing.- There's a copy on GitHub (or similar) in a private repository, and it has today's work in it.
- You can name the last commit where everything worked. If you can't, make one now, once you've checked that it does.
- If your app lives inside an AI builder, you've connected it to GitHub or downloaded the code.
If you're learning the trade
Try it
In the app you've been working on:
- Commit everything with the message "Before the experiment".
- Ask an AI to make a change that touches several files, such as "add a dark mode toggle".
- Run
git statusandgit diff. Write down which files changed and, for one of them, what changed in plain English. - Throw it all away with
git restore --staged --worktree .and delete any new files. Check the app is exactly as before. - Now do it properly: make a branch, ask for the same change, commit it there, and merge it into
mainonce you've read it and it works.
Then undo it with git revert --no-edit and the ID of the commit you made on the branch (git log --oneline shows it). You've now undone a change before you committed it and after, and neither is scary any more.