Git branches create parallel timelines where you can experiment, build features, or fix bugs without touching your stable, production-ready code.
What exactly is a branch?
A branch is a movable pointer to a specific commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.. When you create a branch, Git doesn't copy all your files. It creates a new pointer at your current position. As you make commits on that branch, the pointer moves forward, while other branches stay where they are.
main: A---B---C---F---G (production-ready code)
\ /
feature: D---E---/ (experimental feature)Here, feature diverged after commit B, did its own work (commits D and E), then merged back into main at commit G. That's parallel development that converges when ready.
Why you need branches
Working directly on main is like performing live edits on a website that thousands of people are viewing. Branches give you a safe sandbox.
Isolation
On a feature branch, you can break things, rewrite everything, or change your approach. The main branch remains untouched. If your experiment fails, delete the branch, no harm done.
Collaboration without chaos
With branches, each developer works on their own feature branch:
main: A---B---C---H---I (stable)
/ / /
alice: D---E / (user auth)
bob: F---G (payment system)Neither interferes with the other, and main stays deployable throughout.
main, "just run git add . && git commit && git push" without mentioning branches. In any real project, always create a feature branch first. Direct commits to main bypass code review and can break production.Experimentation and code review
Branches let you try wild ideas risk-free, create experiment/complete-refactor, and if it fails, just delete the branch. They also enable pull requests, where others review your code before it enters main, catching bugs and improving quality.
The default branch: your source of truth
Every Git repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. starts with a default branch. Historically called master, the modern convention is main. This branch represents your official, stable codebase. In professional environments, main is typically protected, all changes must come through reviewed pull requests.
| Branch name | Common usage | When to use it |
|---|---|---|
main | Production-ready code | The default, always stable |
master | Legacy projects | Older repositories |
develop | Integration branch | Git Flow workflow |
feature/* | New features | When building something new |
bugfix/* | Bug fixes | When fixing issues |
hotfix/* | Critical production fixes | Emergency patches |
Check which branch you're on with git branch, the asterisk (*) marks your current active branch.
The branch workflow
The standard workflow used by virtually every development team:
Step 1: Create your branch
git branch feature/user-loginStep 2: Switch to your branch
Creating a branch doesn't switch you to it. You need to check it out:
git checkout feature/user-logingit checkout -b feature/user-login. More on this in the next lesson.Step 3: Work and commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.
Make changes, stage them, and commit. These commits only exist on this branch, main remains unchanged.
Step 4: Switch back and merge
When your feature is complete:
git checkout main
git merge feature/user-loginYour feature is now part of the official codebase.
Best practices
Keep main sacred. Never commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. work-in-progress to main. It should always be deployable.
One branch, one purpose. Don't build three features on the same branch. Smaller branches are easier to review, test, and merge.
Name branches descriptively. Use prefixes and clear descriptions: feature/user-authentication, bugfix/login-error-502, hotfix/security-patch-cve-2024.
Delete merged branches. Once merged, a branch is just clutter:
git branch -d feature/old-branch # Delete locally
git push origin --delete feature/old-branch # Delete remotelyPull before you branch. Always update main before creating a new branch:
git checkout main
git pull origin main
git checkout -b feature/new-featuredevelop, release, and hotfix branches) or GitHub Flow (feature branches only). Master these basics first, and advanced workflows will feel natural.Quick reference
| Command | What it does | When to use it |
|---|---|---|
git branch | List all branches | See what exists |
git branch name | Create a branch | Start new work |
git checkout name | Switch to a branch | Move between work |
git checkout -b name | Create and switch | Most common shortcut |
git branch -d name | Delete a merged branch | Clean up after merge |
# List all branches
git branch
# List all branches including remote ones
git branch -a
# See which branch you're on (marked with *)
# main
# * feature-login ← you're here
# feature-signup