Working directly on the main branch of a GitHub repository is very dangerous as you risk sending buggy code to production. To avoid this, you must create a branch and work on it. That’s how.
What is a branch, anyway?
A branch, in its most basic form, is a copy of a Git project that you can change to your liking and then combine with the original project.
When create a new repository on GitHub , there is a default branch: the “main” branch ( formerly called «teacher» ). This, as the name implies, is the main container where your production code is stored. That is (in most cases, at least), if you push a change directly to the parent branch, you are making a change directly to the work product.
The problem? If you press main directly, you risk sending buggy code to the production environment, which could cause serious problems. This is why you need to create a separate branch to do your work (and then submit that branch for review before merging it with the main branch).
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 in which you would like to create a branch.
Once you have accessed the repository, you will automatically be on the “Code” tab. A little below this, click on 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 must be separated by a hyphen (
-) or an underscore (
Now your new branch is created.
Create a new branch using the command line
At first glance, it may seem easier to use GitHub from a browser, but once you learn to work with GitHub via the command line, things can be done much faster. With that said, you can do just about anything on GitHub with the command line, including creating a new branch.
But before you start, you should clone chosen repository on your local machine. Go ahead and do it now if you haven’t already.
Once done, open the command line application of your choice. Can be Terminal (if you are a Mac user) or Symbol of the system (if you are a Windows PC user), or you can even work from the built-in command line from a text editor, like VSCode .
Regardless of the application you use, you will need to navigate to the repository folder you cloned with the
cd command . From the command line, run this command:
cd <archivo / ruta>
In our example, it would look like this:
Once you are in the proper directory, you can create a new branch. Run this command:
git checkout -b <your-new-branch-name>
<your-new-branch-name> with the real name you want to give your branch.
Your new branch is already created, but it is only available on your local machine. You will need to send it to the source repository by running this command:
git push origin <your-new-branch-name>
Again, replace it
<your-new-branch-name> with the real name of your branch.
You have now submitted 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 on your way to mastering GitHub in no time.