GitHub hero

Working directly in the main branch of a GitHub repository is a very dangerous thing, as you stand the risk of pushing buggy code to production. To avoid this, you should create a branch and work in that. Here’s how.

What’s a Branch, Anyway?

A branch, at its most basic, is a copy of a Git project that you can change as you like and then combine with the original project.

When you create a new repository in GitHub, there’s one branch by default—the “main” branch (previously called “master”). This, as the name implies, is the main container where your production code is stored. That is to say (in most cases, at least), if you push a change directly to the main branch, you’re making a change directly to the working product.

The problem? If you push directly to main, you run the risk of pushing buggy code to the production environment, potentially causing serious issues. That’s why you need to create a separate branch to do your work in (and then later submit that branch for review before it’s merged into the main branch).

RELATED: How Writers Can Use GitHub to Store Their Work

Create a New Branch from the GitHub Website

You can create a new branch directly from the GitHub website. First, open any browser, go to GitHub, and then open the repository that you’d like to create a branch in.

Once you’ve accessed the repository, you’ll automatically be in the “Code” tab. A bit below this, click the button that says “Main.”

A small window will appear. Give your branch a name by typing it in the text box and pressing the Enter or Return key. Words should be separated by a dash (-) or an underscore (_).

Type a branch name in the text box.

Your new branch is now created.

RELATED: How to Switch Branches in GitHub

Create a New Branch Using the Command Line

At first glance, it might seem easier to just use GitHub from a browser, but once you get the hang of working with GitHub via the command line, things can get done so much quicker. That being said, you can do almost anything in GitHub with the command line—including creating a new branch.

But before you start, you’ll need to clone your chosen repository to your local machine. Go ahead and do that now if you haven’t already.

Once that’s done, open the command line app of your choice. This can be Terminal (if you’re a Mac user) or Command Prompt (if you’re a Windows PC user), or you can even work from the built-in command line from a text editor, such as VSCode.

Whichever app you use, you’ll need to navigate to the folder of the repo that you cloned using the cd command. From the command line, run this command:

cd <file/path>

In our example, that would look like this:

Use the cd command to change to the directory of your repo folder.

Once you’re in the proper directory, you can then create a new branch. Run this command:

git checkout -b <your-new-branch-name>

Replace <your-new-branch-name> with the actual name that you want to give your branch.

Run the command to create a new branch.

Your new branch is now created, but it’s only available on your local machine. You’ll need to push it to the origin repository by running this command:

git push origin <your-new-branch-name>

Again, replace <your-new-branch-name> with the actual name of your branch.

Run the command to push your branch to GitHub.

You’ve now pushed your new branch to GitHub!

Working with branches is one of the basics, but it’s also one of the most important GitHub skills to learn. Keep working to master these basics and you’ll be well on your way to becoming fluent in GitHub in no time.

RELATED: What Is GitHub, and What Is It Used For?

Profile Photo for Marshall Gunnell Marshall Gunnell
Marshall is a writer with experience in the data storage industry. He worked at Synology, and most recently as CMO and technical staff writer at StorageReview. He's currently an API/Software Technical Writer based in Tokyo, Japan, runs VGKAMI and ITEnterpriser, and spends what little free time he has learning Japanese.
Read Full Bio »