Git Branches Explained for Beginners
A branch is a safe parallel copy of your project where you can try something without touching the working version. What branches are, the commands to create, switch, and merge them, a simple workflow for solo builders, and why branches matter when an AI agent is editing your code.
You have an app that works. You want to try something risky — a redesign, a new feature, a big change from an AI tool. If it goes wrong, you want the working version back, untouched. That's what branches are for.
What a branch is
A branch is a separate line of work in your Git project. Changes on one branch don't affect the others until you choose to combine them.
Every project starts with one branch, usually called main (older projects use master). By convention, main holds the version that works — the one you deploy.
To try something, you create a new branch from main, make changes there, and when they work, merge them back. If they don't work, you delete the branch and main never knew.
main: A───B───────────E (merge)
\ /
feature: C───D───
Under the hood, a branch is just a label pointing at a commit — which is why creating one is instant, even in a huge project. (Git and GitHub for Beginners covers commits.)
The commands
git branch # list branches; * marks the current one
git switch -c new-checkout # create a branch and switch to it
git switch main # switch back to main
git merge new-checkout # (while on main) bring the branch's changes in
git branch -d new-checkout # delete a branch you've merged
You'll also see the older git checkout -b name and git checkout name. They do the same thing; git switch is newer and clearer.
Commit or stash before switching. Uncommitted changes follow you when you switch branches, which is confusing. Commit them on the branch they belong to first.
Naming branches
Short, descriptive, with hyphens:
add-password-resetfix-checkout-discountredesign-homepage
Some teams add prefixes like feature/ or fix/. Whatever you choose, be consistent.
A simple workflow for solo builders
- Start from an up-to-date
main.git switch main git pull - Create a branch for one piece of work.
git switch -c add-plant-photos - Make changes and commit as you go. (How to Write Good Commit Messages.)
- Push the branch to GitHub.
git push -u origin add-plant-photos - Open a pull request on GitHub, review the changes, and merge. (What Is a Pull Request?.)
- Update your local
mainand delete the branch.git switch main git pull git branch -d add-plant-photos
That's it. The same loop works for every feature, fix, and experiment.
Why branches matter with AI tools
When Claude Code or another agent is changing your project, branches give you a clean boundary:
- Every AI task gets its own branch. If the result is wrong, throw the branch away.
mainis untouched. - You review the whole change at once as a pull request, instead of trusting a stream of edits.
- Preview deployments — a live copy of the app per branch — let you click through the change before merging. (Preview Environments for Every Branch.)
- Several agents can work in parallel on separate branches without trampling each other, using Git worktrees. (Run Parallel Claude Code Agents With Git Worktrees.)
A useful instruction for your AI tool — or your CLAUDE.md: "Never commit directly to main. Create a branch for each task."
When merging goes wrong
If two branches changed the same lines of the same file, Git can't decide which version to keep and stops with a merge conflict. It's normal and fixable. (Merge Conflicts Explained.)
The best prevention: keep branches small and short-lived. A branch merged the same day rarely conflicts. A branch left open for three weeks almost always does.
Common questions
Do I need branches if I work alone? Yes — the main benefit is safety, not collaboration. Branches are how you experiment without risk.
Where did my changes go? You're probably on a different branch. Run git branch to see where you are, then git switch to the right one.
Can I rename a branch? git branch -m old-name new-name.
EasySpawn connects your workspace to GitHub so Claude Code can work on its own branch, and the Pro plan adds preview deployments per branch — so you can try a change live before it touches main. See pricing or join the waitlist.
Related: Merge Conflicts Explained · How to Undo Anything in Git · What Is a Pull Request?
Keep reading
How to Write Good Commit Messages (and Why It Matters With AI)
'fix', 'update', and 'wip' tell you nothing six months later. What a commit message is for, the simple format most teams use, examples of good and bad messages, how often to commit when an AI tool is making the changes, and how to get Claude Code to write useful ones.
How to Write a README for Your Project
The README is the front page of your project — for collaborators, clients, future you, and AI tools that read it for context. What to include, a template you can copy, how it differs from a CLAUDE.md, and the mistakes that make READMEs useless.