Branches
Working on features in isolation

Buzz presents
branches let you try new stuff without breaking the main code, so you can play around safely. no worries at all, friend!branches let you try new stuff without breaking the main code, so you can play around safely. no worries at all, friend!

So far you've been working on one single version of your project. Which is fine... right up until you want to try something new and you're nervous about wrecking the code that already works. That's EXACTLY what branches are for.
What is a Branch?#
A branch is basically a copy of your project where you can mess around freely. Your original stays safe the whole time you're trying new things.
Picture yourself writing an essay.
- You've got your main essay (the "main" branch)
- You want to try out a different introduction
- So you make a copy to experiment on
- If you like it, you keep it, and if you hate it, you toss it
- Your original essay was never in any danger
The Main Branch#
Every repository has a default branch, usually called main (older repositories might call it master). This is treated as the "official" version of your project.
The rule. Keep main working at ALL times. Never push broken code directly to main. My dad's rule was "do it right or don't do it at all," and main is where that rule lives.
Creating a Branch#
Alright, let's make a new branch and try something out.
Step 1. Make Sure You're in Your Repo#
cd ~/Projects/my-first-repoStep 2. See Your Current Branch#
git branchYou'll see * main. That lil star shows which branch you're on.
Step 3. Create a New Branch#
git checkout -b add-featureThis creates a new branch called add-feature and switches you over to it in one move.
Branch naming tips.
- Keep it lowercase
- Use hyphens instead of spaces
- Be descriptive, like
fix-login-bug,add-contact-pageorupdate-styles(future you will be grateful)
Step 4. Verify You're on the New Branch#
git branchNow you'll see this.
main
* add-featureThe star moved to add-feature. You're in.
Working on Your Branch#
From here on, any changes you make land on the add-feature branch. Your main branch stays exactly how you left it.
Step 5. Make a Change#
echo "This is a new feature" > feature.txtStep 6. Add and Commit#
git add feature.txt
git commit -m "Add feature file"Your change is saved on the add-feature branch. Over on main, that file doesn't exist at all.
Switching Between Branches#
You can hop back to main whenever you want.
git checkout mainNow look at your files. feature.txt is GONE! Don't panic... it still exists on the add-feature branch.
Switch back.
git checkout add-featureAaand feature.txt is back. Each branch keeps its own version of your project.
Pushing a Branch to GitHub#
Right now your new branch only exists on your computer. To save it to GitHub, run this.
git push -u origin add-featureThe -u origin add-feature part tells Git to create this branch on GitHub and hook it up to your local branch. After that first push, plain old git push does the job.
Merging Branches#

Happy with your changes? Then it's time to merge them back into main.
Step 1. Switch to Main#
git checkout mainStep 2. Merge Your Branch#
git merge add-featureGit takes the changes from add-feature and combines them into main.
Step 3. Push the Updated Main#
git pushNow main has all the changes you made on add-feature. Donezo!
Deleting a Branch#
Once it's merged, you might not need that branch hanging around anymore.
git branch -d add-featureThat deletes the branch locally, on your computer. To delete it on GitHub too, run this.
git push origin --delete add-featureCommon Branch Workflow#
Here's how branches usually get used, start to finish.
- You're on
mainwith working code - You create a branch for a new feature with
git checkout -b new-feature - You make your changes and commit them on that branch
- You push the branch to GitHub with
git push -u origin new-feature - You create a "Pull Request" on GitHub (that's next lesson)
- After review, you merge the branch into
main - You delete the branch since it's done its job
main: o---o-----------o-----------o (merged!)---o
\ /
new-feature: o---o-------o---------+
(feature work)The top row is main and the bottom row is the new-feature branch. It splits off, picks up a few commits, and then merges back into main.
Quick Reference#
Screenshot this one. You'll be back.
| Command | What It Does |
|---|---|
git branch | List all branches |
git checkout -b name | Create and switch to new branch |
git checkout name | Switch to existing branch |
git merge name | Merge branch into current branch |
git branch -d name | Delete a branch |
git push -u origin name | Push new branch to GitHub |
TL;DR#
- Branches let you build new features without touching
main - The
mainbranch should ALWAYS have working code - Create branches with
git checkout -b branch-name - Switch between branches with
git checkout branch-name - Merging brings changes from one branch into another
- Delete branches after you've merged them (only the capital D version can force out an unmerged one)
What's Next?#
Merging directly works fine, but on a team you usually want somebody to look over your code first. In the next lesson you'll learn about Pull Requests, which is how you propose changes and get feedback before anything gets merged...
This lesson ends with a short activity.
