What is a Git Branch?
A branch lets you work on a new feature without breaking the main code. When it's ready, you merge it back. It's how teams collaborate without stepping on each other's work.
Why branches exist
Without branches, you have one timeline:
Commit 1 → Commit 2 → Commit 3 → Commit 4 (everyone's changes)This causes problems:
- Your half-finished feature breaks the app for everyone
- You can’t easily undo a feature that turned out badly
- Two people editing the same area creates constant conflicts
With branches, you get parallel timelines:
main: Commit 1 → Commit 2 ─────────────→ Commit 5 (merge)
↘ ↗
feature: Commit 3 → Commit 4Your feature branch is isolated. When it’s ready, you merge it back.
Creating and using branches
# See what branch you're on
git branch
# Create a new branch
git branch feature/add-login
# Switch to that branch
git checkout feature/add-login
# Or create and switch in one command
git checkout -b feature/add-login
# Modern Git alternative
git switch -c feature/add-loginNow any commits you make happen on feature/add-login, not on main.
The typical workflow
1. Start from main
git checkout main
git pull # Get latest changes2. Create a feature branch
git checkout -b feature/user-profile3. Make changes and commit
# Edit files...
git add .
git commit -m "Add user profile page"
# More edits...
git add .
git commit -m "Add profile picture upload"4. Push your branch
git push -u origin feature/user-profile5. Open a Pull Request (on GitHub/GitLab)
- Compare
feature/user-profiletomain - Get code review from teammates
- Discuss and make changes if needed
6. Merge when approved
- Click “Merge” in the PR
- Or merge locally:
git checkout main
git merge feature/user-profile
git push7. Delete the branch (cleanup)
git branch -d feature/user-profileBranch naming conventions
Good branch names describe what’s in them:
| Pattern | Example | Use for |
|---|---|---|
feature/xyz | feature/dark-mode | New functionality |
fix/xyz | fix/login-bug | Bug fixes |
chore/xyz | chore/update-deps | Maintenance tasks |
experiment/xyz | experiment/new-api | Testing ideas |
Bad branch names: branch1, test, johns-stuff, asdfgh
Seeing what’s on each branch
# List all branches
git branch -a
# See which branch has which commits
git log --oneline --graph --all
# Output:
# * abc123 (feature/dark-mode) Add dark mode toggle
# * def456 (main) Initial commitMerging branches
When your feature is ready, you merge it into main:
# Switch to the target branch
git checkout main
# Pull latest (someone else might have merged)
git pull
# Merge your feature branch
git merge feature/dark-mode
# Push the merged main
git pushIf there are no conflicts, Git combines the histories automatically.
Merge conflicts
Conflicts happen when two branches edit the same lines. Git can’t decide which version to keep.
git merge feature/dark-mode
# Auto-merging src/styles.css
# CONFLICT (content): Merge conflict in src/styles.cssOpen the file and you’ll see:
<<<<<<< HEAD
background: white;
=======
background: #1a1a1a;
>>>>>>> feature/dark-modeYou decide what to keep:
/* Choose one, or combine them */
background: var(--bg-color); /* I'll make it configurable */Then:
git add src/styles.css
git commit -m "Resolve merge conflict in styles"Conflicts are normal. They just mean two people touched the same code.
Rebasing (alternative to merging)
Rebasing replays your commits on top of the latest main:
# Instead of merge, rebase
git checkout feature/dark-mode
git rebase mainBefore rebase:
main: A → B → C
↘
feature: D → EAfter rebase:
main: A → B → C
↘
feature: D' → E'Pros: Cleaner history, linear timeline Cons: Rewrites commits (don’t rebase shared branches)
Use merge for shared branches. Use rebase for local cleanup.
Remote branches
Branches on GitHub/GitLab are “remote” branches:
# See remote branches
git branch -r
# Fetch all remote branches
git fetch --all
# Checkout a remote branch someone else created
git checkout -b their-feature origin/their-featureWhen you git push -u origin feature-name, you create a remote branch others can see.
Common branching strategies
GitHub Flow (simple)
mainis always deployable- Create a branch for every change
- Open a PR, get review, merge
- Deploy from main
Good for: Small teams, continuous deployment
Git Flow (structured)
main= production codedevelop= integration branchfeature/*branches merge to developrelease/*branches for releaseshotfix/*for emergency production fixes
Good for: Scheduled releases, larger teams
Trunk-based development
- Everyone commits to
mainfrequently - Use feature flags to hide incomplete features
- Deploy from main multiple times per day
Good for: Fast-moving teams with good testing
For vibecoders starting out: GitHub Flow is simplest. One main branch, feature branches, PRs.
Git commands cheat sheet
# Branches
git branch # List branches
git branch feature-name # Create branch
git checkout feature-name # Switch to branch
git checkout -b feature-name # Create and switch
git branch -d feature-name # Delete branch
# Merging
git merge feature-name # Merge into current branch
git rebase main # Rebase onto main
# Remote
git push -u origin feature-name # Push branch to remote
git fetch --all # Get latest remote branches
git pull # Fetch and mergeFurther reading
- What is Git? : The bigger picture of version control
- What is GitHub? : Where branches become pull requests
- What is CI/CD? : Automating tests when you push branches
Frequently asked questions