Branches
Imagine you’re writing a novel. You’ve got your main manuscript, but you want to experiment with a different ending without messing up what you’ve already written. So you make a copy, try out your wild ideas, and if they work, you merge them back into the main story. If they don’t? No harm done - your original is safe.
That’s exactly what branches do in Git. They’re your safe space to experiment, build features, fix bugs, or experiment freely - all without touching the stable version of your code.
What Are Branches, Really?
A branch is a parallel version of your project. When you create a branch, you’re
saying: “I want to work on something without affecting the main codebase.” Every
project starts with a default branch (usually called main or master), and
you can create as many additional branches as you need.
The beautiful part: Branches in Git are incredibly lightweight. They’re not full copies of your project - they’re just pointers to specific moments in your project’s history. This means creating a branch is instant, and switching between them is nearly instant too.
Want to understand pointers and branches? Our Understanding HEAD guide explains what “pointer” means and how Git tracks which branch you’re on.
Working With Branches
See What Branches Exist
Before you start creating branches, it’s helpful to know what already exists.
git branch # Show local branches
git branch -r # Show remote branches (on GitHub, etc.)
git branch -a # Show all branches (local + remote)The branch with an asterisk (*) is your current branch - where your next commit will go.
Create a New Branch
Ready to start working on something new? Create a branch for it.
# Create a branch (but stay where you are)
git branch <branch-name>
# Create a branch AND switch to it immediately
git switch -c <branch-name>Note: The git switch command requires Git 2.23+ (released August
2019). On older versions, use git checkout -b <branch-name> instead.
Naming convention: Use descriptive names like feature-user-login,
bugfix-payment-error, or experiment-new-homepage. Your future self (and your
teammates) will thank you.
Example:
# You want to add a dark mode feature
git switch -c feature-dark-mode
# Now you're on the new branch, ready to codeSwitch Between Branches
Once you have multiple branches, you’ll need to jump between them.
git switch <branch-name>What happens when you switch? Git updates your working tree to match whatever state that branch is in. Files appear, disappear, or change content to reflect that branch’s history. It’s like switching between parallel universes of your project.
Pro Tip: Always commit or stash your changes before switching branches. Git won’t let you switch if you have uncommitted work that would conflict with the target branch.
Delete a Branch
Once you’ve merged a feature or decided an experiment didn’t work out, clean up by deleting the branch.
git branch -d <branch-name> # Safe delete (only if merged)
git branch -D <branch-name> # Force delete (even if not merged)Warning: The uppercase -D is like saying “I don’t care if this work isn’t
saved anywhere else - delete it anyway.” Use it carefully.
A Note About git checkout vs git switch
If you’ve used Git before, you might have seen git checkout used for switching
branches. That still works, but Git introduced git switch as a more focused,
modern alternative.
Why the change? git checkout does many different things (switching
branches, restoring files, creating branches), which can be confusing.
git switch does one thing clearly: switch branches.
For new learners, stick with git switch. It’s clearer and less likely to lead
you astray.
Why Branches Matter
Isolation: Work on features without breaking the main codebase.
Experimentation: Try risky ideas safely. If they fail, just delete the branch.
Collaboration: Multiple people can work on different features simultaneously without stepping on each other’s toes.
Organization: Keep your work organized by topic - one branch per feature, bug fix, or experiment.
Real-World Workflow
Here’s how you might use branches in a typical day:
# Morning: Start work on a new feature
git switch -c feature-shopping-cart
# ... code, commit, code, commit ...
# Afternoon: Boss says "urgent bug fix needed!"
git switch main # Go back to stable code
git switch -c bugfix-checkout-error
# ... fix bug, commit ...
# Later: Back to your feature
git switch feature-shopping-cart
# ... continue where you left off ...Notice how you can context-switch instantly? That’s the power of branches.
What’s Next?
You’ve learned how to create, switch between, and delete branches. But branches are most powerful when you combine them back together - that’s called merging, which we’ll cover in the Everyday Git section.
For now, practice creating branches for different tasks. Get comfortable with the idea that branches are cheap, temporary, and safe. Use them liberally. The worst that can happen is you delete an unused branch.
Curious about how branches actually work under the hood? We explore Git’s branching model in detail in the Concepts section, where you’ll learn that branches are just lightweight pointers to commits. But for now, just use them - understanding the implementation can come later.