Search⌘ K
AI Features

Merge Branches

Explore how to merge branches using the git merge command, including recursive and fast-forward merges. Understand how to effectively combine different commit histories and manage files across branches to maintain a clear project history.

We'll cover the following...

The git merge command

After creating different branches, we need to run git merge to merge the current branch into the other branch as follows:

(master branch)$ git merge feature_a

Its result is a recursive merge. This happens when we merge two branches that have different commit histories.

Afterward, we may check out the feature_a branch and merge it to master:

$ git checkout feature_a
$ git merge master

Its result is a fast-forward merge. This happens when we merge branches that are in the same timeline.

Example of git merge

Let’s look at an example for this merge functionality of Git. In the terminal given below, we have two branches, master and feature_a. The master branch has only file1.txt, whereas the feature_a branch has file1.txt and file2.txt. Moreover, when we run the git log command, we observe that the commit history of both branches is different. The master branch has one commit, whereas the feature_a branch has two commits.

If we merge both branches using master as an active branch, we see that master now has both files and commits of the feature_a branch. They’ve merged into the master branch.

Run the following commands in the terminal below:

$ git checkout master
$ git merge feature_a
$ ls
$ git log
Terminal 1
Terminal
Loading...