Git

What Exactly is a Merge Commit in Git?

While dealing with a large software development project, developers work on multiple branches. They need to track and record the history of changes across several branches. In this situation, the “merge commit” is very useful because it allows them to easily bring all changes from one branch to another branch without losing any history of branches.

This study will discuss:

What is a Merge Commit in Git?

In Git, the “merge commit” is a type of commit that is created when merging two or more branches in a repository. A merge commit combines the changes from multiple different branches into one branch. It usually has at least two parent commits, one for each merged branch. Moreover, it includes all of the changes from the merged branches and the entire branch history.

How to Create/Generate a Merge Commit in Git?

To create a merge commit in Git, first, redirect to the particular local repository. Then, choose the desired branch to be merged and execute the “git merge –no-ff <branch-name>” command. Lastly, check the Git log to view the merge commit.

Step 1: Switch to Desired Repository

First, run the below-provided command and switch to the particular local repository:

$ cd "C:\Git\local_Repo"

Step 2: View Git Log

Then, view the commit history of the current working branch:

$ git log --oneline

It can be seen in the below-provided screenshot the HEAD is pointing to the “5827f21” commit hash:

Step 3: View Available Branches

Next, list the available branches of the Git repository and choose the desired branch that needs to be merged. For instance, we have selected the “alpha” branch:

$ git branch

Step 4: Merge Branches

Now, execute the “git merge” command along with the “–no-ff” option and the particular branch name that needs to be merged:

$ git merge --no-ff alpha

Here, the “–no-ff” option is used to create a commit message even if the branches are fast-forwarded, and “alpha” is our target branch that needs to be merged.

After executing the above-provided command, the default text editor will open up. Enter the desired commit message, save changes and close the editor:

In the below output, it can be observed that the “alpha” branch has been merged with the “master” branch:

Step 5: View Merge Commit

Lastly, check the Git log to view the merge commit message:

$ git log --oneline

It can be observed that the highlighted part is the merge commit message with the “f8db3cf” commit hash:

That was all about the merge commit in Git.

Conclusion

A merge commit is a type of commit created when a user merges two or more branches in the repository. It brings changes/modifications from one branch into another Git branch. It is used to merge changes from different branches into one Git branch. To create a merge commit, the “git merge –no-ff <branch-name>” command is used. This write-up discussed about merge commits and the method for creating a merge commit in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.