3) Collaboration with Git & GitHub Lesson

Git Merge

11 min to complete · By CodingNomads Team

Now that you've practiced using the git branch command, it's time to build on that by delving into another powerful command: git merge.

By the end of this lesson, you'll gain a better understanding of merging by working through the Git Merge Command, including related commands like Git Fast-Forward, Git Squash Commits and more.

What Does Git Merge Do?

The git merge command is used to join the development histories of two or more branches together. Merging is a common practice for developers. Branches are created for a whole array of different reasons: a new feature, testing, a bug fix, and many other reasons. At any time, there will be many branches around, and you will need to merge changes from different branches into other branches.

Merging commits the changes to another location in the Git history. More specifically, it takes the objects of a source branch and combines them with the target branch. Only the target branch is changed in this process. The source branch history remains the same.

The primary use of the git merge command is to combine two branches. It can also be used to merge multiple commits into one history.

A given branch should only be merged into one of two places:

  • The exact branch from which it originated
  • The main branch
Colorful illustration of a light bulb

Info: The main branch is used as a place for your completed and approved work. It is the source of truth for your projects. Merging work into the main branch signifies that it has been tested and approved to be implemented by your team, your customers, or even by the whole world.

Git Merge: Example

Before performing a merge, there are therefore a couple of things you need to take care of first. Here's a look at the steps:

  1. Confirm Target Branch
  2. Git Fetch: Remote Branch
  3. Git Merge: Branches

1. Confirm Target Branch

Run git status to ensure that your repository's HEAD is pointing to the correct target branch. The target branch is the branch you want to merge your changes into.

If your HEAD is pointing to a different branch, you need to run git checkout <Target Branch> to switch to the target branch. For instance, if you want to merge Feature_Branch_A (source) into the main branch (target), you need to first run git checkout main.

2. Git Fetch: Remote Branch

You also need to ensure that the target branch and the source branch are up-to-date with all the latest remote changes. To do so, execute git fetch -a to pull the latest remote commits. Once the fetch completes, ensure the main branch has the latest updates by executing git pull origin main from the main branch.

3. Git Merge: Branches

After you've completed the previous steps, you can initiate a merge by executing git merge <Branch Name>. Here, the branch name is the name of the branch that will be merged into the target branch. Assuming you have successfully checked out the main branch before, this command could look like this:

# merge the Feature_Branch_A (Source) into the main branch (Target)
$ git merge Feature_Branch_A

Now your target branch contains all the changes that have been made in the source branch. If you have completed your work on the Feature_Branch_A, you can now safely delete it.

At the point of merging two branches together, there are two different situations that can occur. Git handles them in different ways:

  • Only one of the branches has changed
  • Both branches have changed

Next, you'll take a look at some different types of merges, which Git applies depending on where changes have been made.

Types of Git Merges

Git Fast-Forward Merge

Suppose you have a repository with two branches, a main branch and a feature branch, which is based on the main branch. If the feature branch has been changed, but the main branch has remained the same, a fast-forward merge can occur.

Here is a visual representation of fast-forward merge:

Visual representation of a fast-forward merge

You can do this because the feature branch is based on the main branch, and the main branch has not changed since then. Therefore, Git only needs to create a pointer on the main branch, which reflects the changes made on the feature branch.

Three-Way Merge

In a similar situation as described above, if both the main branch and the feature branch have changed, then a three-way merge needs to happen. A three-way merge uses a new commit to merge the modifications from two branches together.

Here's a visual representation of a three-way merge:

Visual representation of a three-way-merge

You can see that in this example, there have been commits on both the main and the feature branches. Then, when you do merge your branches, a new commit is created that contains the changes made by both branches.

This concludes the most common ways you will interact with a Git merge. However, included below are some more examples of how merging can fit into a Git workflow.

Colorful illustration of a light bulb

Info: The content below is considered advanced and is not really necessary for entry-level Git use. It is primarily here for your reference.

Git Merge Strategies

When merging branches, there are different strategies that you can consider and that get applied depending on the context and the developer:

Explicit Merge

Explicit merge is the default merge strategy. It's called explicit because it creates a new merge commit, changing the history, and displaying where the merge was invoked.

Implicit Merge

(via git rebase or Fast-forward) An implicit merge does not create a merge commit. It just takes several commits from the specified branch's HEAD and puts them to the top of the target branch. Implicit merges are triggered by rebasing events or fast-forward merges.

Git: Squash Commits

Another type of implicit merge is called squashing, which takes all the target branch's commits and squashes them into a single one. The HEAD of the target branch is then updated with that single commit.

Summary: What Does Git Merge Do?

The git merge command is commonly used by developers to join the development histories of two or more branches together. Merging commits the changes to another location in the Git history.

Git Merge Command Syntax

# merge the Feature_Branch_A (Source) into the main branch (Target)
$ git merge Feature_Branch_A

Types of Git Merges

  • Git Fast-Forward: You can perform a fast-forward merge if the feature branch has been changed, but the main branch has remained the same.
  • Three-Way Merge: A three-way merge uses a new commit to merge the modifications from two branches together.
  • Explicit Merge
  • Implicit Merge
  • Git Squash Commits

For more information, check the git merge documentation.

Git does its best to merge your commits. But just the same, conflicts happen. So, to build on your experience with merging and branching, you're ready to learn the basics of conflict resolution. But first, a video demonstrating Git Merge!