By Suparna GangulyJune 11th 2021

Git Merge is a way of putting back two previously branched lines of development together. When one branch (source) is merged with another branch (destination), all the changes made to the source branch are integrated into the destination branch. Such integration creates a new commit provided the modifications don’t overlap.

The branch merge, usually, takes two commit pointers (branch tips) to identify a common base commit. And when Git identifies a common base commit between the two commit pointers, it creates a new “merge commit”.

The commit created in the destination branch contains everything that the source branch had.

In this tutorial, I’ll take you through the process of merging two branches in Git.

Git Merge Options

  • –commit

–commit is used for committing the result once it’s merged. –no-commit can be overridden through –commit.

  • –edit, -e

These two options open up an editor before committing is done. The auto-generated merge message is edited by it.

  • –signoff

–signoff adds the signed-off-by-line after the entire commit message.

  • –no-signoff

It’s is used for not adding the signed-off-by-line by the committer.

  • –ff

–ff helps resolve the merge faster. This branch pointer matches the merged branch but restricts any new merge commit creation.

  • –no-ff

It’s used in the merge commit creation in each case of the merge. 

  • –ff-only

–ff-only resolves the merge quickly if possible otherwise. And it resists the merge and closes with a non-zero status.

  • –stat

–stat gives a diffstat once the merge is finished.

  • -n, –no-stat

You use these options if you don’t want to display diffstat.

  • –help

–help provides a list containing all the merge options.

  • –overwrite-ignore

You can overwrite all the ignored files through this option. It’s a default behavior where information is taken from the merge result.

Prerequisites to Merge Two Branches in Git

  • GitHub Desktop installed: The GitHub Desktop will let you graphically perform all the tasks related to git. Here are the links to install GitHub desktop for Linux, Windows, and macOS
  • An active GitHub account: If you have a GitHub account, you can practice and check all the commands that we’re using in this tutorial.
  • Local and remote repository: You need a local repository having multiple branches published in the remote server. This local repository will check the commands used.

Merge Two Branches of a Local Repository in Git

The following command checks the branches of the local repository “upload-file”.

git branch

Then, run the command lines given below for merging the content of two branches.

git checkout master

git add upload4.php

git commit -m "Upload text file."

git checkout main

git merge master

Of the above set of commands:

  • The first line will switch the current branch to the “master” branch.
  • The second line will add “upload4.php” in the local repository.
  • The third line adds the commit message.
  • The fourth line switches to the “main” branch.
  • The last line combines the content of the “master” and the “main” branch.

Once the content is merged, in case, the “master” branch needs not to exist in the local repository, you can delete it. To do that execute:

git branch

ls

git branch -d master

ls

git branch

Of the above commands:

  • The first line displays a branch list comprising the present branch.
  • The third line deletes the “master” branch.
  • The last will be executed to check the existing branch list after deletion.

Now, use the following commands.

git checkout -b temp

git add upload5.php

git commit -m “Upload image file”

Of the above code set:

  • The first command creates a new branch “temp”.
  • The second command adds the file “upload5.php” to the repository.
  • The last line commits the task through a commit message.

Merge Two Branches with Conflicts

Often, the changes made in two branches conflict with each other. Whenever a conflict occurs, Git informs you about it. After the merge, the git status command is run. This shows all the conflict-containing files. In such cases, you need to resolve those conflicts before moving ahead with your next action. Generally, you need to resolve the conflicts by printing “both modified:” beside the file names.

Git uses 3 visual indicators, such as <<<<<<<, =======, and >>>>>>> to mark the conflicted content. This helps programmers identify the conflicts during a merge. And it follows this format:

<<<<<<< destination-branch-name

...changes from the destination branch...

=======

...changes from the source branch...

>>>>>>> source-branch-name

Below is an example of a conflict representation.

<<<<<<< main

this is conflicted text from main

=======

this is conflicted text from feature branch

>>>>>>> feature branch;

Once you’ve fixed up the conflicts, you need to run “git add” on the files with conflicts. And then execute a “git commit” for generating the merge commit.

The Conclusion

And that’s how you can merge two branches in Git. I hope you find this tutorial helpful.

)