How do you create a branch in Git?

Branching helps you keep different versions. Each project starts with a master branch. It’s a common practice to create a new branch whenever you want to introduce a new feature that helps separate it from the main code.

To create a branch

Command: git branch [branch_name]

> git branch feature-1

This command creates a new branch named feature-1 from the branch you are currently at.

View branch

To view the branch that you created:

> git branch

This also shows you all of your branches on your local machine.

Switch to another branch

Command: git checkout [branch_name]

> git checkout feature

This command will switch you from the current branch to the mentioned branch in the checkout command.

Create & checkout branch at the same time

You could also create and check that branch out with a single command:

Command: git checkout -b [branch_name]

> git checkout -b feature-1