While you are writing codes there may be scenarios where you will want to achieve a specific functionality or process or goal with another logic of code. But the thing is you don't want to just remove the existing logic right away. You want to check whether your other logic will perform well or not. You want to test it before applying it for real.

For this reason, you can create branches from your main branch or any other branch in git, and make changes in the code which will not affect your codes existing on the main branch but will only reflect on the substitute branch that you created.

Here is one important thing to note is that a new branch will always be created from already existing branch, it may be main branch or any other branch.

Git logo


Creating new branch

The git command for creating branch is simple.

git checkout -b <YouBranchName>

Now suppose you already have a branch in your repository in remote. And, you have to checkout to that branch.

Checkout to an existing branch

I have seen some developers make mistakes (mostly the new devs) while checking out to an existing branch.

What they do is first of all they create a new branch by using 

git checkout -b <YourBranchName> 

command and then they pull the same branch from remote using 

git pull origin <YourBranchName> 

which is a correct way of pulling an existing branch while we are already on that branch and want sync our local and remote repos. But it's not the correct way of checking out to an existing branch. 

If you do this, you will most certainly wind up having merge conflicts in files and if you repo is big, you will face hard time resolving those conflicts. It may also result in headache . 

There is a simpler and shorter code to checkout to an existing branch.

git checkout <YourBranchName>

Yeah, that's it! You just don't have to put -b.