Fast Forwarding While Merging Branches in Git
This tutorial will introduce fast forwarding while merging branches in Git.
Git, a version control system, is used in a collaborative development environment to keep track of the changes done to the files.
In a Git repository, we create many branches from the mainline branch for different purposes. We eventually may merge the changes from these branches into the mainline branch to provide a release build.
Sometimes, the target branch is just fast-forwarded by Git during merging instead of doing an actual merge.
We will now illustrate this with an example.
Fast Forward Merge in Git
In a collaborative development environment, we may create multiple branches from the mainline branch in the Git repository. Say, for bug fixes, we create one branch and say we may create another branch for a feature development purpose.
Eventually, we merge these branches into the mainline branch to integrate the changes of those branches to provide a cohesive build.
Sometimes, we create a branch from the mainline branch, work on that, and do some commits on the new branch. Then, we decide to merge this branch with our mainline branch.
Now, if a linear path is present from the current branch tip to the target branch, the mainline branch, then instead of a merge commit in the target (mainline) branch, a fast forward of the branch happens.
Suppose we have a branch named feature1
; we are merging the changes of feature1
into the main
, our mainline branch.
Now, suppose the HEAD
commit of the main
branch is an ancestor of the commit of the branch feature1
, the one we want to merge.
In such cases, instead of merging the branches, all Git has to do to integrate the histories is to move (i.e.) fast forward the current branch tip up to the target branch tip.
Thus, in our case, with fast forward, the main
branch’s current HEAD
will be moved up to the feature1
branch tip.
Please see below an illustration of the situation of the branches, main
and feature1
, before the merge.
E---F---G feature1
/
A---B---C---D main
Thus, in this case, when we merge the two branches using the git merge
command, a fast forward will happen.
To do a merge, we execute the command as follows.
$ git merge feature1
After executing the command, the main
branch will be fast-forwarded.
Please see the illustration of the fast forward below.
E---F---G feature1, main
/
A---B---C---D
Thus, now the main
branch’s current HEAD
is fast-forwarded. Git will not create a merge commit in this case.
Thus, we have elaborated on fast-forwarding while merging branches in Git.
For more information, please visit -