You accept our Privacy Policy and Terms of Service by using our page
Knowledge Create a new branch on git

Learning the basics of GIT branches and how to create them

Creating a new branch on your local/remote repository is not only useful, it's a need. Each branch keeps it's own commits with own history, let's check on them and learn how to create some.

Basics

First thing we should definitely consider is pulling from origin
$ git pull origin {branch}
Where branch is name of the branch you want to pull from (on remote), usually master.
We can also pull from local origin by
$ git pull
Creating new branch itself
We can create any branch we want to after pulling from origin (not necessary), we can do so by typing
$ git checkout -b {branch_name}

Why are branches important

Branches are part of your everyday development process.
They're very important no matter if you're uploading hot-fix or whole project.
Each branch keeps its own commit and history and serves as 'snapshot' of your changes.
That means that whenever you create a new branch on git you create some sort of snapshot.
This is especially useful when more developers are working together, imagine situation where you wouldn't be able to create a new branch whenever you'd need to.
That'd mean all the developers would be pushing into one branch (most likely master), that could cause code collisions and other issues.

When should I create a new branch?

You should create a new branch whenever you're working on something new.
Let's look at this made-up situation
One of your colleague is working on default design of blog post templates, while you are working on a PHP code for the blog
That means your colleague would work under a branch blog-design and you'd be (or should be) working under a branch blog-code
You both would push the changes, and merge them. You colleague would ask you to make a code for HP (homepage), you'd create a new branch named hp-code.
Everything's going fine, but your colleague notices that the PHP code for blog has some issues in it, you don't need to do anything extra (like re-downloading whole code, changing directories and so on) you'll simply type in
$ git checkout {branch_name}
You'll be able to edit anything that has to be edited from there, push it and continue working on your HP code by checking into its branch.

That's it! You now know the basics about creating a new branch on GIT.