Skip to content

Join the Seedly owners community →

GitHub

Branches

Working on features in isolation

Written by 12 min read1 activity
Buzz, your presenter

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!

Buzz sits at the fork of a tree branch that splits in two, pointing at the new branch
A branch lets you try new work without touching main

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-repo

Step 2. See Your Current Branch#

git branch

You'll see * main. That lil star shows which branch you're on.

Step 3. Create a New Branch#

git checkout -b add-feature

This 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-page or update-styles (future you will be grateful)

Step 4. Verify You're on the New Branch#

git branch

Now you'll see this.

  main
* add-feature

The 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.txt

Step 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 main

Now look at your files. feature.txt is GONE! Don't panic... it still exists on the add-feature branch.

Switch back.

git checkout add-feature

Aaand 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-feature

The -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#

Buzz braids a green rope and an amber rope together into one
Merging brings your branch's changes back into main

Happy with your changes? Then it's time to merge them back into main.

Step 1. Switch to Main#

git checkout main

Step 2. Merge Your Branch#

git merge add-feature

Git takes the changes from add-feature and combines them into main.

Step 3. Push the Updated Main#

git push

Now 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-feature

That deletes the branch locally, on your computer. To delete it on GitHub too, run this.

git push origin --delete add-feature

Common Branch Workflow#

Here's how branches usually get used, start to finish.

  1. You're on main with working code
  2. You create a branch for a new feature with git checkout -b new-feature
  3. You make your changes and commit them on that branch
  4. You push the branch to GitHub with git push -u origin new-feature
  5. You create a "Pull Request" on GitHub (that's next lesson)
  6. After review, you merge the branch into main
  7. 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.

CommandWhat It Does
git branchList all branches
git checkout -b nameCreate and switch to new branch
git checkout nameSwitch to existing branch
git merge nameMerge branch into current branch
git branch -d nameDelete a branch
git push -u origin namePush new branch to GitHub

TL;DR#

  • Branches let you build new features without touching main
  • The main branch 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.