Creating new branches in git

Branches are used to develop the code independent of main branch. Main advantages of Git branch are –
  • separates main development work from small tasks.
  • You can easily context switch between different tasks.
  • Allows you to work on specific features or functionalities of the application. You merge your work in main branch only when your branch code is finished and working properly.
  • Allows you to provide hot bug fix easily without affecting what you are doing currently.
To create a branch, you can use below command. It will create a branch with name x. Notice that below command will only create a new branch. That branch will not be checked out in your working copy.
git branch x
To check out above branch in working copy, you have to use below command.
git checkout x
To create a new branch and check it out in working copy at the same time, you have to use below command. It will create new branch b2 and check it out.
git checkout -b b2
creating new branches and checking out in Git After a branch is created and checked out, you can make changes to files, add them to staging area or commit them. Once you are finished with all your changes, you can merge your branch in to main branch. You can also create a branch off specific commit using below syntax.
git branch branch-name <commit Id>
git branch branch-name HEAD~3
git checkout -b new_branch v1.2 : creating branch off tag
Here is the output of above example.

Complex problems, Simple Solutions