Chapter 8
Version Control: Git and GitHub with Claude Code
In the previous chapters we asked Claude again and again to "commit the current state to git", and promised to explain later. The time has come.
Git is like a "save game" button for your project. Every time you save, Git remembers exactly what every file looked like at that moment. If something breaks later, you can go back to any earlier save point. GitHub is a website that keeps a copy of your project in the cloud, so it doesn't get lost, so you can share it with others, and so it can be deployed (chapter 9).
The good news: you don't need to memorize Git commands. Claude knows them very well. You need to understand what you want, and what to watch out for.
A short glossary
| Term | What it is, simply |
|---|---|
| Repository (repo) | A project folder that Git tracks |
| Commit | A save point with a short message describing what changed |
| Branch | A parallel copy of the project where you can try things without touching the main version |
| main | The "official" version of the project, the one that goes live |
| Diff | A list of exactly what changed: lines added and lines removed |
| Remote | The copy of the project on GitHub |
| Push | Sending commits from your computer to GitHub |
| Pull | Downloading changes from GitHub to your computer |
| Pull request (PR) | A formal proposal to merge a branch into main, with a chance to review and comment before merging |
| Merge | Combining the changes from one branch into another |
Step 0: Install and configure Git
Check whether Git is already installed
git --versionIf you see something like git version 2.47.0, skip to configuration. If not:
winget install Git.GitOr download Git for Windows and install it with the defaults.
winget install Git.GitOr download Git for Windows and install it with the defaults.
On a Mac, this command offers to install Apple's developer tools, which include Git:
xcode-select --installOn Linux (Ubuntu / Debian):
sudo apt install gitAfter installing, close and reopen the terminal, and check again with git --version.
Tell Git who you are
Every Git commit is signed with a name and an email. You set this once per computer. Do it yourself, in a regular terminal, with your own details:
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"Part 1: Commits and tidy messages
Start a repository
A Next.js project (like in chapter 6) already comes with Git. In any other project, ask:
Initialize git in this folder, make sure there's a .gitignore suitable for the project (excluding node_modules and .env files), and commit the current state as a starting point
"What changed?"
Before committing, it's always worth knowing what's about to be saved:
What changed since the last commit? Summarize it for me file by file, one sentence per file
Behind the scenes Claude will run a command like git status. Here's what its output looks like:
On branch main
Changes not staged for commit:
modified: app/page.tsx
modified: lib/recipes.ts
Untracked files:
components/SearchBar.tsxmodified means an existing file changed, and Untracked means it's a new file Git doesn't know yet.
Commit messages that say something
A commit message is a letter to your future self. A month from now, when you're looking for "when did we break the search?", the messages are what will help you find it.
Weak prompt
Make a commit
Strong prompt
Look at all the changes and commit them to git. The message should start with a short line describing what changed and why, followed by a short list of the main changes. If there are unrelated changes, split them into separate commits.
Here's what good messages look like next to weak ones:
update
fix
changes
asdfAdd search and category filter to home page
Fix servings calculator rounding to 1 decimal
Increase ingredient list font size for readabilityLook at the history
Show me the last 10 commits, and briefly explain what happened in each
a3f9c21 Add search and category filter to home page
7be4d10 Add recipe page with ingredients and steps
19c0e85 Set up layout and Nunito font
e6d2b47 Initial commit from Create Next AppEach line is a save point. The odd number at the start is its "ID". You don't need to remember it, but you can ask by it: "What changed in commit 7be4d10?".
Part 2: Branches, a safe place to experiment
Why work in a branch?
Say you want to try a completely new design for the site. If you work straight on main and change your mind halfway, you'll have a mess. In a separate branch, main stays exactly as it is, and if the experiment didn't work out, you simply throw the branch away.
Rule of thumb: every new feature or experiment gets its own branch.
I want to add the ability to mark recipes as favorites. Create a new branch called feature/favorites from main, switch to it, and tell me when we can start
$ git switch -c feature/favorites
Switched to a new branch 'feature/favorites'Now all work is saved in the feature/favorites branch. The current branch name also appears in Cursor's bottom corner, so you always see where you are.
Switch between branches
Commit what we did in this branch, and take me back to main. I want to see that the original site hasn't changed
Refresh the browser: the site is back to main's state. Ask to switch back to feature/favorites, and the changes return. That's the magic of branches.
Part 3: See exactly what changed
Before something goes into main, you need to know exactly what it is. There are three ways, from the most visual to the most textual:
1. Source Control in Cursor (Ctrl + Shift + G): a list of files, and a side-by-side comparison with one click. The easiest on the eyes, as we learned in chapter 5.
2. The /diff command inside Claude Code: shows all the changes in the folder, including the ones Claude made in the current conversation.
/diff3. Ask Claude for a summary, which is the most useful way when there are lots of changes:
Compare the current branch to main and summarize for me: which files changed, what the feature does now, and what might break because of these changes. Be honest, even if it's code you wrote
Here's what a raw diff looks like, in case you want to peek:
--- a/components/RecipeCard.tsx
+++ b/components/RecipeCard.tsx
@@ -12,6 +12,10 @@ export function RecipeCard({ recipe }: Props) {
<h3 className="text-xl font-bold">{recipe.title}</h3>
+ <FavoriteButton slug={recipe.slug} />
<p className="text-gray-600">{recipe.time} minutes</p>
- <span>{recipe.category}</span>
+ <span className="rounded-full px-3">{recipe.category}</span>Lines with + were added, lines with - were removed. Lines without a sign are context, so you understand where it happens.
Part 4: Put it on GitHub
What you need
- A GitHub account: sign up for free at github.com. You do this yourself.
- GitHub CLI (
gh): a tool that lets Claude work with GitHub, meaning create repositories and pull requests:
winget install GitHub.cliwinget install GitHub.clibrew install ghOn Linux, instructions for each distribution are at cli.github.com.
- Sign in: in a regular terminal (not inside Claude), run this command and follow the instructions. It opens a browser for approval:
gh auth loginCreate a GitHub repository and push
Create a new private repository on GitHub called grandma-recipes using gh, connect it to this project, and push the main branch. At the end give me the link to the repository
✓ Created repository your-username/grandma-recipes on GitHub
https://github.com/your-username/grandma-recipes
✓ Added remote https://github.com/your-username/grandma-recipes.git
✓ Pushed commits to https://github.com/your-username/grandma-recipes.gitOpen the link in your browser: all your files are there.
Part 5: Pull requests and code review
What is a pull request, and why do you need one if you work alone?
A pull request (PR) is a formal request: "here's a branch with changes, I propose merging it into main". On GitHub it gets its own page, with a description, all the changes and room for comments.
Even when you work alone, a PR is a deliberate pause before something enters the official version: a place to review, run checks and think once more. And in chapter 9, once the site is connected to Vercel, every PR also gets its own preview URL, so you can see the change live before it reaches the real site.
The full process
| # | Step | The command you'll see |
|---|---|---|
| 1 | A new branch from main | git switch -c feature/favorites |
| 2 | Work and commits, several times | git commit |
| 3 | Push the branch to GitHub | git push -u origin feature/favorites |
| 4 | Open a PR | gh pr create |
| 5 | Review and fixes | /code-review, and more commits as needed |
| 6 | Merge into main | The Merge button on GitHub |
| 7 | Update main on your computer | git switch main then git pull |
You don't need to type any of these commands. They're here so you'll recognize them in Claude's approval prompts.
Open a PR
When the feature is ready and committed in its branch:
The favorites feature is ready. Push the branch to GitHub and open a pull request into main. In the description: what the feature does, why we added it, which files changed, and how to test it manually step by step. If there are risks or things you didn't test, write those too
Claude will use gh pr create, and at the end return a link to the PR page:
Creating pull request for feature/favorites into main in your-username/grandma-recipes
https://github.com/your-username/grandma-recipes/pull/3Weak prompt
Open a PR
Strong prompt
Open a PR into main. In the description: what changed and why, how to test manually, and what the risks are. Show me the description before you create the PR.
Code review: Claude checks Claude
Here are three ways to get a review, from the simplest to the most formal:
1. A regular request, in a new conversation. After /clear, Claude looks at the changes with fresh eyes, without being attached to code it wrote itself:
Review the changes in the current branch against main like a strict senior developer. Look for bugs, unhandled edge cases, accessibility problems and mobile layout issues. Show a list ordered by severity, with references to file and line, and don't fix anything yet
2. The built-in /code-review command (or /review for short): reviews the current changes and looks for bugs, in a separate agent with a clean context.
/code-reviewYou can also review a PR by its number, for example /code-review 3, and add --comment so the comments are posted directly on the PR on GitHub.
3. A security review with /security-review: checks the changes in the branch against main and looks for security problems, like exposed sensitive data or unchecked input. Especially recommended before merging anything involving forms, sign-in or saving data.
/security-reviewAfter the review, decide what to fix:
Fix the high and medium severity issues from the review, commit each fix separately with a clear message, and push to the same branch. The PR will update on its own
Merge
Do the merge yourself, on GitHub: open the PR page, go through the Files changed tab to see all the changes, and if everything's good, click Merge pull request and then Confirm merge.
After merging, update your computer:
I merged the PR on GitHub. Switch back to main, pull the updates from GitHub, and delete the local feature/favorites branch since it's already merged
Part 6: Safety belts
Git is a great safety net, but a few commands can tear holes in it. Here they are, and how to build protection in several layers.
The commands to know (so you don't approve them)
| Command | What it does | Why it's dangerous |
|---|---|---|
git push --force (or -f) | Pushes and overwrites what's on GitHub | Deletes commits from GitHub that you don't have, for example someone else's work or commits from another computer |
git reset --hard | Returns the folder to an earlier commit | Every uncommitted change is deleted, with no way to recover |
git clean -fd | Deletes files Git doesn't know about | New files that weren't committed yet are deleted entirely |
git checkout -- . / git restore . | Discards all uncommitted changes | Unsaved work is lost |
git branch -D | Deletes a branch even if it wasn't merged | Work saved only in that branch may be lost |
git commit --amend after a push | Rewrites a commit that was already pushed | Local history and GitHub's history diverge, and then a force push is "needed" |
Layer 1: your eyes
In manual mode, every command goes through you, and you read before approving, just like in chapter 3.
In auto mode, Claude Code's safety mechanism blocks dangerous commands like force push, git reset --hard and git clean -fd by default. It also respects boundaries you set in the conversation. If you write "don't push to GitHub without asking me", the mechanism blocks pushes until you say otherwise.
Layer 2: rules in CLAUDE.md
## Git
- Never git push --force, and never push directly to main.
- Every feature in its own branch, and merge into main only via a pull request.
- Before any command that discards changes (reset, clean, restore), stop and ask.
- Commit messages: a short first line describing what and why.This strongly influences Claude's behavior, but it's guidance, not a lock. For a real lock you need the next layers.
Layer 3: permission rules in Claude Code
In the project's settings file you can define commands Claude Code always blocks (deny), and commands it always asks about (ask), in every permission mode, including auto mode:
{
"permissions": {
"deny": [
"Bash(git push --force *)",
"Bash(git push -f *)",
"Bash(git push --force-with-lease *)",
"Bash(git reset --hard *)",
"Bash(git clean *)"
],
"ask": [
"Bash(git push *)"
]
}
}deny: these commands are blocked, period.ask: any othergit pushwill ask for your approval, even in auto mode.
You can also ask Claude to create the file:
Create a .claude/settings.json file in the project, with permission rules that block git push --force, git push -f, git reset --hard and git clean, and that ask for approval before any other git push. Explain each line to me
And you can view and edit the existing rules at any time with the /permissions command.
Layer 4: protecting main on GitHub
This is the strongest layer, because it's enforced on GitHub's server. It doesn't matter who sends the command or how it's worded: Claude, you, or anyone else.
On the repository page on GitHub:
- Click Settings (in the repository's top menu).
- In the side menu, under Code and automation, choose Rules → Rulesets.
- Click New ruleset → New branch ruleset.
- Give it a name, for example
protect-main, and set Enforcement status to Active. - Under Target branches click Add target → Include default branch.
- Check the rules:
- Restrict deletions: main can't be deleted.
- Block force pushes: main can't be overwritten.
- Require a pull request before merging: changes enter main only via a PR.
- Click Create.
From now on, if anyone tries to force push to main, GitHub refuses:
remote: error: GH013: Repository rule violations found for refs/heads/main.
remote: - Cannot force-push to this branch
! [remote rejected] main -> main (push declined due to repository rule violations)And if something gets deleted anyway?
Git almost never truly forgets. Even after a reset or deleting a branch, old commits stay for a while in an internal log called the reflog. If something happened:
I think I lost some commits. Don't change anything yet. Use git reflog to find the state before the last operation, and explain what you see and how you recommend restoring it
Notice the "don't change anything yet": in an emergency, the first thing is to understand, and only then to act.
Exercise: a complete feature, the professional way
We'll work on the recipe site from chapter 6 (or any other project with Git) and add favorites: a heart button on every recipe card, and a page showing only the recipes you marked.
- Safety belts: create
.claude/settings.jsonwith thedenyandaskrules from part 6, and add the Git rules to CLAUDE.md. - GitHub: if the repository isn't on GitHub yet, create it and push main (part 4).
- Branch: ask Claude to create
feature/favorites. - Work: ask for the feature:
Add a heart button on every recipe card and on the recipe page, which marks the recipe as a favorite. Favorites are saved in the browser. Add a “My favorites” link to the header that leads to a page showing only the marked recipes, and if there are no favorites, a friendly message. Commit each step separately with a clear message
- Check: in the browser, and in Claude's summary of the changes against main (part 3).
- PR: ask it to push and open a pull request with a full description.
- Review: run
/code-review, and fix what matters. - Merge: merge it yourself on GitHub, then ask Claude to update main on your computer.
- Protection test (only if you enabled protection on GitHub): ask Claude to "try pushing a small change directly to main". GitHub should refuse and require a PR.
Finished the exercise?
0 of 9
Summary
- Git keeps save points, and GitHub keeps them in the cloud. You don't need to memorize commands: ask Claude, and understand what you're approving.
- Good commits: one topic per commit, with a message explaining what and why.
- Branches for every feature or experiment. main stays clean.
- Seeing what changed: Source Control in Cursor,
/diff, or a summary from Claude. - Pull request: branch → PR with a description →
/code-reviewand/security-review→ a merge you do yourself. - Four safety layers: your eyes, CLAUDE.md,
deny/askrules in.claude/settings.json, and protecting main on GitHub.
In the next chapter we'll deploy the site with Vercel: every push to main updates the site automatically, and every pull request gets its own preview URL.
Check yourself
1. What's the strongest layer against force pushing to main?
2. Why work in a branch and not straight on main?
Answering every quiz question marks the chapter as done automatically.