Git Branching Support

Branching is a major part of any modern version control system. It enables developers to diverge from the main line and continue development without messing with the main line code.

A branch in GIT is simply a pointer to one of the commit snapshot. The default branch in Git is called as ‘master’. When we create new commits in GIT the master branch moves forward automatically and points to the latest commit snapshot.

Git also maintains a special pointer called as ‘HEAD’, which points to the current branch. This is helpful in case we have created multiple branches.

Git Branching Support

Create and Checkout a New Branch

Git provides the git branch command to create a new branch, shown below:

git branch <branch_name>

Example:
git branch poc

Now in order to start working on the poc branch, we need to switch from master to poc. Git provides the 'git checkout' command to do so. This will move 'HEAD' from master to poc.

git checkout poc

Note: We can use the '–b' option with 'git checkout', as a short hand to create and checkout a branch. It does two things, creates a new branch and checkout the branch.

git checkout –b poc

Note: Git checkout does two things. First, it moves the HEAD to the branch we checked out. Second, it refreshes the working directory to the branch checked out, which means the working directory will now have files from the commit snapshot that the checked out branch is pointing at.


Merging

In Git we can merge one branch into another after we have finished working on it.

To understanding merging let's take a simple example. Consider that you need to start working on a poc, so you create a new branch called 'poc'. While working on the poc you did some commits and finally you completed your work on the poc. Now at this point we do not need the 'poc' branch any more. We can merge our changes to the master branch and delete the 'poc' branch.

Considering that we are currently on the 'poc' branch, below are the commands we need to execute to merge poc into master:

//First we need to checkout master
git checkout master
//Now we can merge poc into master
git merge poc
//Now we can delete poc
git branch -d poc

Shown below is the pictorial representation:

Git Merge 1
Git Merge 2
Git Merge 3
Git Merge 4

Git Merge on Diversed Branch

But this may not always be the case. Consider the case where while working on the poc, you come to know that you need to deliver a fix for an issue on priority. At this point you might checkout master and create a new branch say 'bug100', work on it, fix the bug and deliver. In doing so the master will move forward and after the poc is complete the state of the snapshots would be as shown in the image below:

Git Merge 5

Now as our poc is complete we would want to merge poc into master and delete the poc branch. But this time its not straight forward since our master branch has diversed as a result of the emergency fix we delivered. In this case when we merge poc into master, Git will automatically create a new snapshot that contains the changes of the poc as well as the emergency fix. This snapshot is called as 'merge snapshot'.

Git Merge 6

Merge Conflict

Merging may not always go smoothly. We may sometimes encounter a merge conflict when the same files have been modified in both the branches we are trying to merge. Consider that as part of the emergency fix we changed a particular file called 'Abc.java' and we also made changes to the same file as part of our poc. Now when we try to merge poc into master, Git will not be able to do a clean merge since 'ABC.java' has been modified in both branches. Thus we will encounter a merge conflict. At this point we will have to resolve the conflict manually before we can merge the branches.


Git Rebase

We can also use git rebase as an alternative to git merge. Git rebasing basically patches the changes introducted in an earlier commit to the present snapshot. Considering our example in the previous section, when we use rebasing instead of merge, Git will patch the changes introduced in 'Snapshot 5' to 'Snapshot 6' and create a new snapshot as shown in the below image:

Git Merge 7

The final snapshot produced is the same as in case of merge. The only good thing here is that the commit history is clean and linear.

Below are the commands required to rebase poc on master branch:

git checkout poc
git rebase master
RELATED ARTICLES

Git Overview

GIT is an open source distributed version control system that emphasizes on speed and efficiency. This article covers some of the basics of the GIT VCS and also discuss about the GIT config tool.

View Article

Creating and using GIT Repository

A GIT repository is where GIT stores all the project files. We can either create a new repository or clone an existing one. This article shows how to create/clone a repository and few basic operatio..

View Article

Tagging in GIT

Tagging allows us to mark a specific point in the commit history or snapshot. A tag is typically used to mark a project release. This article shows how to create tags in Git.

View Article