git branch

Branch management in git. Learn how to create, delete, and list branches.

Why do we need branching? Every software development group has a mainline of code or a master branch. There are always chances that a new feature should be done and commit to the repository without disturbing the mainline. In git, after creating a repository creates the master branch.  To do branching there is a git branch command.  With git Not only we can create a branch, but it also can do full branch management.

Management means creating a new branch, delete a branch, list, etc. We assume that we have created a repository using git init and have a master branch to work with.

The Mainline keeps also moving along with more commits done in the mainline. When development work completes in the new branch, the branch is merged into the mainline.

There can be further branching from a branch.

How to create a git branch on Linux step by step?

  • Before creating a branch, clone the central git repository on the local machine.
# git clone root@
Initialized empty Git repository in /home/gittutorial/myrepository/.git/
root@’s password:
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
  • List down the branches, with the git branch command
[root@CentOS_6_64-160 myrepository]# git branch
* master
Till now only the master branch is present.  With the git branch command, we will create a new branch named branch1 and again use the git branch command to list all branches.
[root@CentOS_6_64-160 myrepository]# git branch branch1
[root@CentOS_6_64-160 myrepository]# git branch
* master
Now you can see that there are 2 branches one is master and the other is branch1. The working branch is master,  git checkout command, switches to a branch.  Following commands example shows switching branch from master to branch1.
[root@CentOS_6_64-160 myrepository]# git checkout branch1
Switched to branch ‘branch1’
[root@CentOS_6_64-160 myrepository]# git branch
* branch1
If a user will do any changes and commit that will be in branch1 and the head of branch1 will be moved, but the master will be pointing to the original head.

Create a git branch from a particular commit point:

There are chances that a master branch is still not stable and a developer needs to branch the master over a stable version for new development. Git provides a make a branch from a particular commit. A commit has a unique hash code. Git log command is for displaying all commits done along with hash code. Users can get the hash code for the point from where the new branch will start.

[#]git branch new-branch 55206d46ae57179c117a74e10da797dcbd15177a

The git show branch gives more detailed information for all branches.

Deleting an existing branch :

Git provides the option for deleting a branch. But that branch should not be a current working branch. If a user tries to delete the current branch git gives an error “Can not delete the <branch name>, is current branch”. A user has to first switch the branch by using the git checkout command.

[#]git branch -d branch-name

Merging a branch with the master:

Git branch command provides the feature to merge a branch to the current branch. If branch one needs to be merge in master the commands are

[#]git checkout master

[#]git merge branch1

How to push a branch to a central repository?

The branch created till now is in the local repository. If other developers want to make the clone from the branch, they can not as the branch is not visible in the remote/central repository. To make a branch in the remote repository, then the branch needs to push. The first switch to the new branch with git checkout then, do push with command.

[#]git push origin branch-name

Leave a Comment