Git branches confuse everyone at first. The problem isn't the concept — it's that most tutorials jump into theory without showing you the practical workflow. Let's fix that with real examples.
What Is a Branch? (One Sentence)
A branch is a separate copy of your code where you can make changes without affecting the main version. When you're happy with your changes, you merge them back.
Why Use Branches?
- Safety: Experiment without breaking working code
- Organization: Keep different features separate until they're ready
- Collaboration: Multiple people can work on different features simultaneously
- Review: Others can review your changes before they go into the main codebase
The Basic Workflow (99% of What You Need)
# 1. Make sure you're on main and up to date
git checkout main
git pull
# 2. Create a new branch for your feature
git checkout -b add-login-page
# 3. Make your changes (edit files, add features)
# ... write code ...
# 4. Stage and commit your changes
git add .
git commit -m "Add login page with email/password form"
# 5. Push your branch to GitHub
git push -u origin add-login-page
# 6. Create a Pull Request on GitHub
# (or merge locally — see below)
# 7. After PR is merged, clean up
git checkout main
git pull
git branch -d add-login-page
Branch Naming Conventions
Use clear, descriptive names:
feature/add-search-bar— new featurefix/broken-login-button— bug fixupdate/homepage-copy— content updaterefactor/user-authentication— code restructuring
Avoid vague names like my-branch, test, new-stuff, or fix2.
Merging Branches
Option A: Merge Locally
# Switch to the branch you want to merge INTO
git checkout main
# Merge your feature branch
git merge add-login-page
# Push the updated main
git push
Option B: Pull Request on GitHub (Recommended)
- Push your branch:
git push -u origin add-login-page - Go to your repository on GitHub
- Click "Compare & pull request"
- Add a description of your changes
- Click "Create pull request"
- After review, click "Merge pull request"
Handling Merge Conflicts
Conflicts happen when two branches change the same line. Git marks them like this:
<<<<<<< HEAD
const title = "Welcome to My Site";
=======
const title = "Welcome Home";
>>>>>>> add-login-page
To resolve:
- Open the conflicted file
- Choose which version to keep (or write a combination)
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>) - Stage and commit:
git add . && git commit -m "Resolve merge conflict"
Common Mistakes and Fixes
"I committed to main by accident"
# Move the commit to a new branch
git branch my-feature
git reset --hard HEAD~1
git checkout my-feature
# Your commit is now on my-feature, main is restored
"I need to switch branches but have uncommitted changes"
# Option 1: Stash your changes
git stash
git checkout other-branch
# ... do work ...
git checkout original-branch
git stash pop
# Option 2: Commit as work-in-progress
git add .
git commit -m "WIP: save progress"
git checkout other-branch
"I need changes from main in my feature branch"
# Rebase (cleaner history)
git checkout my-feature
git rebase main
# Or merge (easier, keeps history)
git checkout my-feature
git merge main
Viewing Branches
# List local branches (* marks current)
git branch
# List all branches (including remote)
git branch -a
# See a visual log of branches
git log --oneline --graph --all
Deleting Branches
# Delete a local branch (safe — won't delete if unmerged)
git branch -d branch-name
# Force delete (even if unmerged)
git branch -D branch-name
# Delete a remote branch
git push origin --delete branch-name
Rules of Thumb
- Never commit directly to
main(always use branches) - One feature per branch (don't mix unrelated changes)
- Keep branches short-lived (merge within a few days)
- Pull from main regularly to avoid big conflicts later
- Delete branches after merging (they're archived, not gone)