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.
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).
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 (
Your new branch is now created.
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:
In our example, that would look like this:
Once you’re in the proper directory, you can then create a new branch. Run this command:
git checkout -b <your-new-branch-name>
<your-new-branch-name> with the actual name that you want to give your 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>
<your-new-branch-name> with the actual name of your branch.
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.