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):
In this session, execute git checkout -b $branchName
Replace $branchName with the desired name, e.g. abc/feature-1
The success is confirmed like this:
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
.
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:
And that´s it. When you check on the GitHub website you see your newly created branch:
GitHub Website
To create a new branch via the GitHub website navigate to your repository and make sure your are on the Code
tab:
Click on the branch selector (here main is preselected). You should see this:
Type the new branch name into it, here I use abc/feature-2
Click on Create branch: $branchName
GitHub will reload the page and show you something like this:
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
:
After that you can use your new branch by checking it out, git checkout abc/feature-2:
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: