Git Basics for AI-Assisted Coding
Git isn't optional once an AI tool is editing your code for you — it's the only reliable way to review, commit and roll back changes.
Why Git matters more, not less, with AI
When you write every line yourself, you remember roughly what changed. When an AI agent edits five files in ten seconds, you don't — and you have no way to prove it, or undo it cleanly, without version control. Git is what turns "I hope that was fine" into "I can see exactly what changed and revert it in one command."
Step 1: Initialize a repository
git init
git add .
git commit -m "Initial commit"Step 2: Set up a .gitignore before your first commit
Do this before committing, not after — once secrets or dependency folders are committed, removing them from history is a much bigger job.
node_modules/
.env
.env.local
.next/
dist/
*.logStep 3: Commit after every AI-driven change, not once a day
Small, frequent commits give you fine-grained rollback points. If an AI tool makes three changes in a row and the third one breaks something, you want to be able to go back to exactly after the second one — not to the start of your afternoon.
git add .
git commit -m "Add pricing page generated with AI, reviewed and tested"Step 4: Review the diff before you trust it
This is the single most important habit in this tutorial. Reading a diff takes thirty seconds and catches problems before they're committed — reading them after they've broken production takes much longer.
git diff
git diff --staged # after you've run `git add`, to see what's about to be committedStep 5: Use branches for anything you're not sure about
git checkout -b feature/pricing-page
# make changes with your AI tool, commit as you go
git push -u origin feature/pricing-page
# open a pull request to review the full diff before merging to mainStep 6: Undo a bad AI change
- Uncommitted changes you don't want: `git restore .`
- One specific file: `git restore path/to/file.ts`
- Undo the last commit but keep the files as they are: `git reset --soft HEAD~1`
- Undo the last commit completely, discarding the changes: `git reset --hard HEAD~1` (only when you're certain)
Step 7: Connect to GitHub
git remote add origin https://github.com/yourname/your-repo.git
git branch -M main
git push -u origin mainTools used in this tutorial
Claude Code
A terminal-based agentic coding tool from Anthropic that reads, edits and runs code in your own repository.
Cursor
An AI-native code editor built on VS Code, with deep inline editing, chat, and agent capabilities.
OpenAI Codex
OpenAI's cloud and CLI coding agent that can work on tasks in an isolated sandboxed environment.
Prompts to pair with this
Related tutorials
Getting Started with Claude Code
Install Claude Code, set project-level conventions, and complete your first real task inside an existing codebase.
BeginnerHow to Debug AI-Generated Code
A repeatable method for tracking down bugs in AI-generated code — reproduce, isolate, prompt with real context, and verify with a test.
IntermediateHow to Deploy a Next.js App Built with AI
Take a Next.js project from your local machine to a live, publicly accessible URL with environment variables handled correctly.
IntermediateReady to build the next one?
Browse the full tutorial library or grab a ready-made prompt for your next step.