The Mental Model
A soundproof room for experiments.
Imagine your live site is the album that's already on streaming. A branch is a private recording booth where you can experiment without anyone hearing.
What users see. Always stable. Never experiment here.
Your private space. Break things freely. Nobody else sees it.
When the feature is ready, merge it back into main. Now everyone hears it.
Feature is merged. Delete the branch. Keep the repo tidy.
* f4a1b2c v1.1 live (HEAD -> main)
* e3d9a7f Merge feature/dark-mode
|\
| * a3f2c1d add dark mode toggle
| * b7e9f0a add theme context
| * c1d4e8b install next-themes
|/
* d5a2b3c deploy v1.0
* e8f1c2d fix mobile nav
* f3a5d7e initial commit
Branch Basics
Open the Booth
Start a new branch from main. Your experiments begin here.
Change Rooms
Jump between branches. Each has its own version of the code.
Release the Track
Combine your feature branch back into main.
Clean the Booth
Feature is merged. Delete the branch to keep things tidy.
The Merge
Two versions become one.
When your feature is ready, you merge it back. Git combines both timelines. Usually this is seamless.
Sometimes two people edit the same line. That's a merge conflict. Git can't decide which version wins—you have to pick.
Clean Merge
No conflicts. Git auto-combines. You're done.
Merge Conflict
Same line edited in both branches. Pick which one to keep.
Pro Tip
Merge main into your feature branch regularly to avoid big conflicts later.
<<<<<<< HEAD (main)
backgroundColor: 'white'
=======
backgroundColor: '#0a0a0a'
>>>>>>> feature/dark-mode
// Pick one. Delete the markers.
// Then: git add . && git commit
The Safety Net
Undo Anything
Git is a time machine. Every commit is a save point. You can always go back.
// Undo last commit (keep changes)
git reset --soft HEAD~1
// Discard all unsaved changes
git checkout .
// Go back to a specific commit
git revert <commit-hash>
The Naming Convention
Good branch names tell you what's being built at a glance.
feature/dark-modefix/mobile-nav-overflowrefactor/extract-user-hookmy-branchtest123stuffCommon Patterns
✗ Branch Traps
"I'll just code on main"
One bad push and your live site is broken. Always use branches for new work.
"I'll merge when it's 100% done"
Long-lived branches = massive merge conflicts. Merge small and often.
"I don't need branch names"
You will forget what "test2" was for in 3 days. Name descriptively.
"I'll fix the conflict later"
Conflicts compound. Resolve immediately or they snowball.
✓ Clean Branches
One branch per feature
Dark mode gets its own branch. Auth gets its own branch. Never mix.
Pull before you push
git pull origin main first. Make sure you're up to date.
Use PRs for merging
Pull Requests give you a review step before code hits main. Use them even solo.
Delete merged branches
A clean repo has 1-3 active branches, not 47 stale ones.
The Exercises
Practice the branch workflow.
The Branch
Create & Switch
- →Create a new feature branch
- →Make a small change and commit
- →Switch back to main—change is gone
The Merge
Combine Work
- →Switch to main
- →Merge your feature branch
- →Delete the merged branch
The Conflict
Resolve It
- →Edit the same line on two branches
- →Try to merge—see the conflict
- →Resolve it manually and commit
📋 Quick Reference Cheatsheet
New Branch
git checkout -b feature/xMerge
git merge feature/xDelete Branch
git branch -d feature/x