We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Branches

You might be primarily familiar with using Git in a single-user environment, pushing updates through a linear workflow. For example:

# working off of the "main" branch locally
git add .
git commit -m "undo Lane's giant mistake"
git push origin main

This works quite well for single developer side projects, but it doesn't work well when you're working within a team setting. What happens when a teammate makes changes to the same function you're working on and you both push directly to the main branch? What happens if you want a teammate to review your code before it's merged? What happens if you want to work on multiple features at once?

Branches solve these problems.

What Is a Branch?

A branch is (basically) a copy of your codebase. However, it's a special kind of copy that makes merging new code changes from one branch to another simple and easy.

So far, you may have been only working on the main branch, but you can create as many branches as you want, and they're a great way to keep changes that are unrelated to each other isolated and contained.

In many teams, the main branch reflects the state of the codebase that's running in production. This means that the main branch should always be stable and ready to deploy. If you want to:

  • Add a new feature
  • Fix a bug
  • Refactor some code

Then you should create a new branch to add those changes to. This allows you to work on those changes independently without affecting the main branch.

Assignment

  1. Check which branch you're currently on with git branch. You'll see a list of branches, with an asterisk next to the branch you're currently on:
* main
  1. Create a new branch called addtests. I like to name my branches after the change I'm about to make, and in this case, we're about to add tests.
git switch -c addtests
  1. When you create a new branch, it only exists locally. Push this new branch up to GitHub:
git push origin addtests

You can check on GitHub to make sure the branch exists.

Paste the URL of your GitHub repo into the box and run the GitHub checks.