Create a GitHub branch

Problem

You need to create a GitHub Branch? Your employer uses GitHub and you need to create a new branch on one of your projects?

Solution

Overview

There are multiple solution which allow you to create a branch on GitHub. I will point out two of them: Using the Git commandline and the GitHub website.

Both are equal in the sense that they result in a new branch on GitHub. They are just different approaches for different ways of thinking or processes.

I assume in this article that you have a repository already created on GitHub and at least one file in your repository.

Git

Open a command line and change into the folder where your local git repository is. For me it looks like this (Mac OS, iTerm, zsh, oh-my-zsh theme):

Create a GitHub branch Folder with Git repository

In this session, execute git checkout -b $branchName

Replace $branchName with the desired name, e.g. abc/feature-1

Create a GitHub branch Checkout new branch

The success is confirmed like this:

Create a GitHub branch Checkout new branch successful

Now you would make changes are required. Here we skip this step.

To push your new branch to GitHub execute git push -u $remoteName $branchName

$remoteName is the name in your git config for your connection to GitHub. Normally it is origin .

$branchName is the again the name of your branch.

So for our example I use git push -u origin abc/feature-1 .

Create a GitHub branch Push new branch to GitHub

Depending on your configuration you will get asked for your GitHub credentials or for the password where your stored it (my config on Mac OS asks me for the Systemuser Password which I use for normal logins).

If all went well, you get this success Message:

Create a GitHub branch Push new branch to GitHub successful

And that´s it. When you check on the GitHub website you see your newly created branch:

Create a GitHub branch Verify new branch available

GitHub Website

To create a new branch via the GitHub website navigate to your repository and make sure your are on the Code tab:

Create a GitHub branch Code Tab

Click on the branch selector (here main is preselected). You should see this:

Create a GitHub branch Branch Selector open

Type the new branch name into it, here I use abc/feature-2

Create a GitHub branch Branch Selector new branch name

Click on Create branch: $branchName

GitHub will reload the page and show you something like this:

Create a GitHub branch Branch created

And you created a new branch via the GitHub website.

To be able to use the new branch you need to pull all updates from your repository with git fetch or git pull:

Create a GitHub branch Pull new branches from Github

After that you can use your new branch by checking it out, git checkout abc/feature-2:

Create a GitHub branch Use new branch

Congratulations, you can now use your new branch.

Note

You can´t create a branch out of nothing. It always has a parent. That means, when you create a branch from e.g. master (or main as the new GitHub default), that means that your new branch has the same code as the parent.

Let me know when it helped you.

Best,

Frank

Sources:

https://forum.freecodecamp.org/t/push-a-new-local-branch-to-a-remote-git-repository-and-track-it-too/13222

https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/creating-and-deleting-branches-within-your-repository#creating-a-branch

Leave a Reply