Git Merge

Learn via video courses
Topics Covered

Overview

Git merge is used to merge multiple commits of various branches into a single branch. We perform branching for parallel development of new feature(s) and bug fixing. Once the bug is fixed or the new feature is added, we can merge these branches into a single branch. The git merge command can also be considered as an alternative to the git rebase command which is also used to merge branches. For merging branches, git takes the head points of the commits and it will first find the common base commit among the branches and will perform a merge commit to combining the changes of each commit sequence. There are five types of merges in git, they are: Fast Forward, Recursive, Ours, Octopus, Resolve, and Subtree.

Pre-Requisite

The prerequisites for learning the git merge command can be a basic understanding of Version Control Systems, Branching, and Git. Let us discuss them briefly before learning about the git rebase command.

Version Control Systems

A version control system is a tool in software development that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. There are two types of Version Control systems namely - Centralized Version Control Systems (CVCS) and Distributed Version Control Systems (DVCS).

Git

Git is a version control system that tracks the changes in the code, documents, and other important information regarding a certain code base (or project), etc. Git is free and one of the most widely used version control systems. We can use Git through the command line as well as through its graphical user interface (GUI). The command line or terminal version of Git is known as Git Bash on the other hand the GUI version of Git is known as Git GUI. Git tracks the changes in a project and saves a certain state that is known as commit. A commit is a snapshot of the file's current version (s). So, we track these commits and can revert to a certain commit if we want.

Branching

A brach is an independent line of development that is used to add certain features and fix bugs without hampering the main project. So, we can develop new features in parallel and when the development is completed, we can add the back to the main project.

The git branch command enables us to perform parallel development. The command can create, rename, list, and delete branches.

Introduction to Git Merge

Git merge is one of the most widely used commands that is used to merge multiple commits (that may be stored in several branches) into a single branch. We perform branching for parallel development of new feature(s) and bug fixing. Once the bug is fixed or the new feature is added, we can merge these branches into a single branch.

Now, for merging several branches, the git associates a series of commits into a unified history.

Let us now look at some of the features of the git merge command:

  • Git merge command creates history in a graphical form so that the user can understand the commits and merging.
  • We can use the git merge command on both private and public branches.
  • The git merge command preserves the merging history.
  • The git merge command shows us all the merge conflicts in a single place. Refer to the Merge Conflict section to learn about merger conflicts in merging.
  • Git merge command merges two safe branches.

The git merge command is also considered an alternative to the git rebase command. Now the major difference between the git rebase and git merge is that when we perform git rebase then the commits will be merged linearly, on the other hand, if we use the git merge command then all the commits will be merged in a time altogether.

Note: The git merge command is often used with the git checkout command and the git branch -d command. The git checkout command is used to select a certain branch, on the other hand, the git branch -d command is used to delete an obsolete branch.

How Does It Work?

As we have discussed that branching helps in parallel development while our main branch (or master) branch is in production. Now, to understand the working of the git merge command, suppose a master branch is there in the production and we have created other branches. Now, if we create some commits in the newly created branch (let's say feature branch) then git will create new pointers to these commits for tracking changes.

Since the latest commits of the feature branch is ahead of the commits of the master branch, we can use the git merge command to merge the commits of the feature branch to the master branch.

Refer to the image provided below for a better understanding. how git merge works

Git will take the head points of the commits and it will first find the common base commit among the branches and will perform a merge commit to combining the changes of each commit sequence.

Preparing to Merge

Now to perform the merging, some preparation steps need to be followed. Let us discuss them briefly getting into the essential commands of git merging.

  1. Confirm the receiving branch. We will first check whether the HEAD of the branch is pointing to the correct merging branch or not. The checking can be performed using the git status command that is used to check the status of the repository. If we need to change the branch then we can use the git checkout command to move to a certain branch.

  2. Fetch the latest remote commits. Before merging, we need to first check if our receiving branch is up-to-date with the latest changes in the remote repository. For fetching the latest changes (to update the current branch), we can use the git fetch command. After fetching, we will use the git pull command to ensure that our master branch has the latest updates.

  3. Merge After performing the above steps, we can now initiate the merging by using the git merge command.

Essential Commands to Perform Git Merging

Let us now look at the various commands that are used in git merging.

The basic git merging command:

  1. The command to merge a certain commit of the active branch.
  1. The command to merge a certain branch.

Types of Merges

We have six types of merging strategies in git, let us discuss them briefly.

Fast Forward

When there is a linear path of commits present from the current branch to the tip of the master branch then we use the fast-forward merging strategy. So, in such a scenario git has to only integrate the current branch tip to the target branch's tip i.e. master branch's tip. It is one of the most commonly used and one of the most efficient merging strategies as git only combines the histories of all the target branches. Git can easily access all the commits that are reachable from the target branch can be easily accessed through the current branch.

The syntax for performing fast-forward git merge is:

Recursive

Recursive merging is used in the case of two heads. It is the default strategy of merging used when we well or merge one branch. It can detect as well as handle the merging involving renames but it cannot detect copies.

The syntax for performing recursive git merge is:

Ours

Ours is the merging strategy that is usually used when we have a very old branch that we are trying to get merged into and we do not want to deal with any kind of merge conflict. We can tell git to use the current version of the branch and just move on.

The **syntax **for performing our git merge is:

Octopus

The octopus merging strategy deals with scenarios where there are more than two heads are present. Octopus merging strategy cannot deal with complex merging (the merging that needs manual resolutions). The main aim of using this merging strategy is to bundle the topic branch heads together.

The syntax for performing octopus git merge is:

Note: Octopus merging strategy is the default merging strategy used during pulling and merging more than one branch.

Resolve

The resolve strategy is built to deal with two heads only, the first one being the head of the current branch and the other one being the head of the branch that we have pulled from. The resolve merging strategy is considered to be a fast and safe merging strategy as it carefully detects the criss-cross merge ambiguities present in the commit(s).

The syntax for performing resolve git merge is:

Note: Resolve merging strategy uses a 3-way merging algorithm for merging branch heads.

Subtree

Subtree merge is considered the modified version of the recursive merging strategy. Suppose that we have two branches A and B. Instead of reading the tree structure formed at the same level, B first adjusts to match the structure formed by A. This adjustment is also performed by the ancestor tree structure of the tree.

The syntax for performing subtree git merge is:

Note: We have one more type of merging strategy called the Manually call named merge strategy.

Merge Conflict

Merge conflict is an issue that arises when we try to merge two branches that have been edited at the same time and in the same file (refer to the image provided below for more clarity), then git will not be able to merge them as git cannot identify the version that needs to be used for changes.

In case of a merge conflict, Git stops the merging process just before performing the merge conflict so that the user can resolve the conflict manually.

Merge Conflict

Now to resolve the merge conflict, we use the git merge tool. The git merge tool is an editor that provides us with several options that are useful in merging the git conflicts.

The command to open the git merge tool is:

A window will open where the user can modify the changes to resolve the conflicted file. Press the i key and perform the required changes and then press the ESC key to come out of the insert mode. Finally type :w! at the bottom of the editor to save the file state and exit from the file.

The conflict is shown with <<<<<<<. Git shows us the change from the base branch using the divider ======= followed by >>>>>>>>> branch-name.

When we will delete the conflict makers, i.e. <<<<<<<, ========, >>>>>>>, we can make the final change that we want to merge.

Conclusion

  • Git merge is used to merge multiple commits of various branches into a single branch. The branching is performed for parallel development of new feature(s) and bug fixing.
  • The git merge command can also be considered as an alternative to the git rebase command which is also used to merge branches.
  • There are five types of merges in git, they are: Fast Forward, Recursive, Ours, Octopus, Resolve, and Subtree.
  • For merging branches, git takes the head points of the commits and it will first find the common base commit among the branches and will perform a merge commit to combine the changes of each commit sequence.
  • The git merge command shows us all the merge conflicts in a single place. Refer to the Merge Conflict section to learn about merger conflicts in merging.
  • Git merge command creates history in a graphical form so that users can understand the commits and merging.
  • Git merge command can be used for private as well as public branches.
  • In case when the linear path of commits is present from the current branch to the tip of the master branch, we use the fast-forward merging strategy.
  • Recursive merging is used in the case of two HEAD and it is the default strategy of merging used when we pull or merge one branch.
  • Ours merging is used when we have a very old branch that we are trying to get merged into and we do not want any merge conflicts.
  • Octopus merging strategy deals with scenarios where there are more than two heads present.
  • The resolve strategy is used with two heads only, the first one is the head of the current branch and the other is the head of the branch that we have pulled from.