GIT: Working with Branches in git

GIT: Working with Branches in git

There are many things to mention about branch in Git, however I will document just a few minimal details related to working with branch in Git.

The basic commit operation performed on a Git repository is on a single branch. In terms of Git, this is called the master branch.

However, while working on parallel development for a code it is better to create different branches under Git.

GIT: Working with Branches in git

How Branch function under Git?

In Git, every repository is nothing but a tree of commits. The initial commit for a repository is the root for the tree. All the subsequent commit will have one or more parents. So this creates a reference path leading up to the initial commit for the repository.

Every commit can be uniquely identified by a 40 character hash. Each commit represents a hash under Git. An example of a hash can be 9645e696d16afe805d5e4aaa5524270b73d13b03. The commit hash is unique for the entire Git repository.

The references for the commit hash are stored in a file. On local repository, the branch commit has is stored under refs/heads/. For a repository with single branch, you will find the file refs/heads/master with the hash commit as it’s content. A repository with multiple branches will have file with all the branch names under that folder.

Using branch in Git

To begin with, lets check the current branches for the repo.

[[email protected] my_repo]# git branch


[[email protected] my_repo]# git branch
* master

Note the asterisk shown in the output. It signifies the current branch you are on.

To create a new branch you need to use the following syntax.

git branch

Let’s create a branch version_1

[[email protected] my_repo]# git branch version_1

List the branch again.


[[email protected] my_repo]# git branch
* master

So we now have a new branch version_1, with the contents from the master branch.

Switch to a branch using the following syntax.

git checkout

Let’s move to branch version_1

[[email protected] my_repo]# git checkout version_1
Switched to branch 'version_1'

Verify the branch you are on now.

[[email protected] my_repo]# git branch
* version_1

You can now start modifying the contents under a different branch which works well in a parallel development cycle.

A small trick to create and checkout in the new branch is:

git checkout -b

This command will create a NEW_BRANCH with the connects from the current branch you are on.

You will find the new files created for the new branch under refs/heads/. To begin with the content for the new branch and master will be the same as it is using the contents from master. After the commit to the branch, the content will change.

Once the changes or modifications to the NEW_BRANCH look acceptable to be merged into master or any other branch, you can always use git merge.

Delete branch in Git

And you can always delete a branch in Git.

The following is the syntax to delete a branch in Git.

git branch -d

So if I was to delete the branch version_1, I will use the following command.

git branch -d version_1


[[email protected] my_repo]# git branch -d version_1
Deleted branch version_1 (was 4f26h78).

Please note that if you are “on” a branch you cannot delete it.

So if I am on branch version_1 and I try to delete it, I will get something like the following:

[[email protected] my_repo]# git branch -d version_1
error: Cannot delete the branch 'version_1' which you are currently on.

You can verify the branch you are on currently using:

[[email protected] my_repo]# git branch

And, change to some other branch (or master branch) before you delete the branch.

GIT: Working with Branches in git

No Comments

Post a Comment

Time limit is exhausted. Please reload CAPTCHA.