Git Basics for AI Coders
Save your work, undo mistakes, and collaborate, without memorizing commands.
In this chapter
- What Git is and why every AI coder needs it
- The 5 commands that matter (and what they do in plain English)
- How to undo mistakes when a bad prompt breaks everything
- How AI tools like Claude Code handle Git behind the scenes
What is Git?
Imagine Google Docs version history: every time you edit a document, Google saves a snapshot. You can see what changed, who changed it, and go back to any previous version.
Git is the same thing, but for your code. Every time you save a snapshot (called a "commit"), Git remembers exactly what your project looked like at that moment. You can go back to any save point whenever you want.
Why You Need It
Your AI tool generates code for you. Most of the time it works great. But sometimes one bad prompt can mess up your entire project: your layout breaks, your data disappears, or your pages stop loading.
Without Git, you have no way to go back. You would need to remember what your code looked like before and manually undo every change. With Git, you just tell it "go back to my last save point" and everything is restored.
Without Git
One bad AI prompt breaks your nav bar. You try to fix it, but now the footer is broken too. You spend 2 hours trying to get back to where you started.
With Git
One bad AI prompt breaks your nav bar. You run one command and your project is back to the way it was 5 minutes ago. Total time: 10 seconds.
The 5 Commands You Actually Need
Git has hundreds of commands, but you only need five. Your AI tool runs them for you most of the time, but knowing what they do helps you understand what is happening.
Commits = Save Points
A commit is like saving your progress in a video game. Each save point has a short description so you know what happened at that moment.
Good commit messages matter. When you need to go back, you will scan through your save points looking for the right one. Clear messages help you find it fast.
Good commit messages
- "Add contact form to homepage"
- "Fix broken nav bar on mobile"
- "Change background color to blue"
- "Add Stripe payment integration"
Bad commit messages
- "update"
- "fix"
- "asdf"
- "stuff"
Branches = Parallel Universes
Imagine you have a working website. You want to try adding a completely new feature, maybe a dark mode toggle or a payment system. But you are not sure it will work, and you do not want to break what you already have.
A branch is like making a photocopy of your entire project. You work on the copy. If it works out, you merge the copy back into the original. If it does not work, you throw the copy away. Your original stays untouched either way.
How branches work in practice
- Your main project lives on a branch called
main - You create a new branch called
add-dark-mode - You experiment freely on that branch. Nothing affects
main - If it works, you "merge" the branch back into main. If not, you delete the branch.
GitHub = Cloud Backup
Git lives on your computer. GitHub is the cloud version. Think of it like the difference between a Word document on your desktop vs. the same document saved in Google Drive.
Undoing Mistakes
This is the most important section. When an AI prompt breaks your project, here is how to get back on track.
βI just made changes that broke my project. How do I undo all uncommitted changes and go back to my last commit? I'm using [your AI tool]. Please walk me through it step by step.β
βI need to go back to a previous version of my project from earlier today or yesterday. Show me my recent commits and help me check out the right one. Please explain each step in plain English.β
βI have uncommitted changes that I want to save temporarily but not commit yet. How do I use git stash to set them aside and get them back later? Please explain step by step.β
Git in AI Tools
The good news: most AI coding tools handle Git for you behind the scenes. Here is what each tool does automatically.
.gitignore: Your "Do Not Track" List
Some files should never be saved to Git. Your secret passwords, massive dependency folders, and local settings files: these should stay on your computer only.
A .gitignore file tells Git to ignore specific files. It is like a "do not track" list. Anything listed in this file will not be committed or pushed to GitHub.
βCheck my .gitignore file and make sure it includes .env.local, node_modules, and .next. If I do not have a .gitignore file, create one with all the standard entries for a Next.js project.β
Real World Example: Recovering from a bad AI prompt
You ask your AI tool to redesign your entire homepage. It makes changes across 15 files and now nothing looks right. Instead of trying to manually undo each change, you use Git to jump back to your last commit: the version before the AI made changes. Your whole project is restored in seconds.
βI asked my AI tool to redesign my homepage and it broke everything. Help me use Git to go back to my last working version. Show me my recent commits and help me restore the right one. I'm using [Claude Code / Cursor / other tool].β
Common Git Errors (in Plain English)
These error messages look scary, but they are easy to fix. Here is what each one means.
Your branch is behind 'origin/main'
What it means: Someone else (or you on another computer) pushed changes to GitHub. Your local copy is outdated.
How to fix it: Run git pull to download the latest changes before continuing.
Merge conflict in [filename]
What it means: Two people (or you and your AI tool) changed the same lines in the same file. Git does not know which version to keep.
How to fix it: Ask your AI tool to resolve the conflict. Say: "I have a merge conflict. Help me pick the right version."
Detached HEAD
What it means: You are looking at an old version of your project but not on any branch. It is like standing in a museum looking at an old painting: you can see it but you are not "working" on it.
How to fix it: Run git checkout main to go back to your main branch.
Failed to push: rejected
What it means: GitHub has changes that you do not have locally. You need to download them first before uploading yours.
How to fix it: Run git pull first, then try git push again.
Prompt This
Copy these prompts and paste them into your AI tool whenever you need Git help.
βI just created a new project. Help me initialize Git, create a .gitignore file with the right entries for a Next.js project, connect it to a new GitHub repository, and push my first commit. Walk me through every step.β
βI want to try adding [describe feature] to my project without breaking what I have now. Help me create a new Git branch, make the changes there, and then merge it back into main when it works.β
βShow me what files have changed since my last commit and explain what each change does in plain English. I want to understand what my AI tool modified before I commit.β
Frequently Asked Questions
Do I need to memorize Git commands?
No. Your AI tool handles Git for you most of the time. But understanding what Git does helps you fix problems and avoid losing work. Think of it like knowing how a car works. You do not need to be a mechanic, but knowing what the gas pedal does helps.
What happens if I forget to commit?
Nothing bad happens immediately. But if you make a mistake later, you will not be able to go back to that point. It is like playing a video game without saving. If you lose, you start over from your last save.
Can Git break my project?
Git itself will not break anything. It only tracks and saves changes. If you accidentally run a destructive command, Git usually has a way to undo it. When in doubt, ask your AI tool for help before running any Git command you do not understand.
Do I need a GitHub account?
You do not strictly need one, but it is highly recommended. GitHub is free and gives you a cloud backup of your code. If your computer dies, your code is safe on GitHub. It is also required if you want to deploy to Vercel or collaborate with others.