Merging in GIT
Merging is an important part of
version-control. It is an integral part of GIT. Some questions arise when we get started with git.
What is the need for Merging?
Types of Merging?
Is Merging necessary when I am working alone in a project?
Note: In Git, HEAD is a pointer which points to the latest commit in the current working directory.
Need for Merging
When people work on a project, everyone doesn't work on the same feature/bug/piece of code. Different people work on different parts of code. Some work on a bug, while some work on a new feature. But, there will be a single branch that holds the production level code. After going through different levels of testing, it is
merged with that branch.
Some don't follow testing methods, which leads to many errors and bugs; which must be resolved separately. We must test the features, very-important parts of code, and most frequently accessed parts of code.
If someone has fixed a bug and you want that change to be available at production, then you'll have to merge that person's branch with the production branch (of course after testing). You can merge any branch with any other branch.
When merging, you have to make sure that you are merging the incoming branch from the receiving branch, ie if you want to merge the feature into master; You have to first switch to master and then merge the feature branch.
git merge <branch_name> is the command to merge the incoming branch.
Types of Merging
There are two types of merging:
- Fast Forward Merge
- 3-Way Merge
Fast Forward Merge
This is a simple merge. Formally, FF Merge is a type of merge which happens when there are no new commits in the original branch at the time of merge.
For example, let us assume there are 2 branches
feature. The feature branch has made some changes, meanwhile, the master branch has not changed at all. It is the same when the feature branch was created ie, the latest commit during the creation of the feature branch and during merging is the same.
Now, when we switch to the master branch and try to merge the feature branch, a fast-forward merge will be done. What happens internally is that the HEAD just points to the feature branch's latest commit. No new commit is created. The following looks like this
So, any further commits will be continued from the HEAD pointed commit.
Simulating Fast-Forward Merge:
git init -- Committing in master -- echo "file1" > file1.txt git commit -a -m "commit in master" git checkout -b new-branch -- Committing in new-branch -- touch file2.txt echo "file2" > file2.txt git commit -a -m "commit in new-branch" -- switching to master and merging new-branch -- git checkout master git merge new-branch
When Fast-Forward Merge is not possible, 3-way-merge is done. This means that 3-way-merge is done when some changes are committed after creating a new branch.
Let's consider the same example, with master and feature branches. But this time, after creating the feature branch, some changes were committed to the master branch.
We have to note that during fast-forward-merge, we did not create a new commit. We just moved the HEAD pointer to the feature branch's latest commit. In 3Way-Merge, We'll create a new commit that holds both the changes from the master branch and the feature branch. The result looks like,
git init -- Commit in master & create a new branch -- echo "file1" > file1.txt git commit -a -m "commit in master" git checkout -b new-branch -- Commit in new-branch -- touch file2.txt echo "file2" > file2.txt git commit -a -m "commit in new-branch" -- Creating a new file and committing in master after creating a the new branch which leads way to 3Way Merge -- git checkout master echo "file3" > file3.txt git commit -a -m "new commit after creating branch" -- Merge new-branch with master -- git merge new-branch
Is Merging required when working alone?
Yes, of course. Working alone doesn't mean that you won't be using branches. Also, it is a good practice to use branches for new features and handle a single main branch for production/stable code. So yes, when you use Git; most probably you'll be needing to merge.
So, this is all about merging. This looks simple until merge-conflict arises!