It took me a while to understand the importance of branches and how they work.
To most developers, this must sound all too familiar. That’s why I’m going to break it down for you. No more confusion and no more double-checks.
Branches in GitHub (and Git) allow you to take the original codebase, create an exact replica of it, make changes, and then submit your changes to be merged into the original code base.
This way, you can make sure the final code base is always bug-free. Or at least not disastrous.
In our previous articles, we covered some of the basic git commands and saw how you can improve your development process with git workflows.
In this article, I will cover:
- How Branches in GitHub and Git Work
- Creating a New Branch From GitHub Website
- Creating a New Branch Using GitHub Desktop App
- Using Command Line to Create New Branch in GitHub
How Branches in GitHub and Git Work
GitHub (and Git) enables you to control which version of the code gets deployed to your production.
And branches play a fundamental role in this.
Every time you deploy code to production, you’ll obviously want to make sure you’re only deploying the most stable version.
But how do you make sure you always have a stable version ready for deploy when you’ve got multiple teammates constantly working on new features and bug fixes?
By creating multiple branches.
Every repository (ie. your code base) in GitHub can have multiple branches.
The main branch, often called the Master branch, is the official stable version of your code. This is the branch that gets deployed to production.
And it’s also the branch that’s never edited directly. Especially when you’re working collaboratively.
So when you want to make changes, you’d create a new branch from the master branch, make your changes in it, and when you’re ready, you’d request your changes to be merged into the master branch.
Here’s a visual example of how working with multiple branches might look like:
Git workflow with feature and develop branchesWhich brings us to the question of how to create a new branch in GitHub?
There are three ways you can create a new branch in GitHub:
- Using GitHub's website.
- Using GitHub's desktop app.
- Using command line.
Let’s dive in and look at all three of them.
Creating a New Branch From GitHub Website
Navigate to the main page of the GitHub repository for which you want to create a new branch.
You’ll see the name of your current branch in the branch selector dropdown. To see all the branches in the repository, click on [NUMBER Branches]. In my case, you can see that I’m currently on Master branch and I have 2 branches.
Viewing all branches in your GitHub repositoryTo create a new branch, click on the branch selector dropdown and start typing the name of the new branch in the text box.
Type the name of your new GitHub branch in the search boxBy default, this text box checks if there’s an existing branch with the name you just entered.
If not, you’ll be able to create a new branch by selecting “Create Branch”.
Click create branchThis will automatically create a new branch with the Master branch as your base branch.
However, to switch and edit your code from the new branch you just created, you’ll still have to use GitHub’s desktop app or the terminal code. That’s why, most developers prefer to just use the desktop app or work using terminal commands.
Let’s see how you can do that as well, shall we? :)
Creating a New Branch Using GitHub Desktop App
Creating a new branch on the GitHub Desktop app is identical to how you’d create on the website.
Make sure you’ve got GitHub's desktop app downloaded before reading further.
Once you’ve got the desktop app open, navigate to the repository in which you want to create a new branch.
Now, click on the branch selector dropdown and click "New Branch" button.
Click the "New Branch" button from the Branch Selector drop downIn the create new branch pop-up, enter the name of your new branch and click the “Create Branch” button.
Enter the name of your branch and click "Create Branch"Alternatively, you can use the shortcut key Cmd + Shift + n
(or Ctrl + Shift + n
if you’re on Windows) to create a new branch.
To begin making changes to this branch, simply hit Cmd + Shift + a
(or Ctrl + Shift + a
if you're on Windows) to open your code in your code editor.
It's important to remember that while a new branch might be created, it will not be accessible to everyone unless you publish it to the remote location. You can choose to make all the code changes, commit them, and then publish your branch. Or, you can click on the “Publish” button.
Using Command Line to Create New Branch in GitHub
Creating a new branch using the command line is nearly every developer’s favourite method. Because once you get a hang of it, it’s unbelievably easy to use.
First, make sure to cd
into your local repository. Once you’re in the right folder, execute
$ git branch <branch-name>
This will create a new branch. But before you start making changes to your code, you will have to first switch to the new branch you just created. To do that, run
$ git checkout <branch-name>
Many developers, especially when they’re just getting started, forget switching to the new branch. That’s why you can use this command that will create the new branch and immediately switch you to it:
$ git checkout -b <branch-name>
Once you’ve created a new branch and switched to it, you can start making changes in your code. However, all of these changes (including the new branch) is still only in your local machine.
To publish the new branch you created in GitHub and make it available for everyone in your team, run the following command:
$ git push -u <remote> <branch-name>
With Branches in Git and GitHub, working on code collaboratively is a lot easier than it used to be. While there can still be inefficiencies in your development workflow, a lot of it depends on the intricacies of it.
However, if I must leave you with one thing, never directly make changes to your master branch. Nothing good has ever come off it.