How To Carry Out Complex Git Merge Tasks

Once you invoke this command, the commit history removes later commits and resets the history to the referenced ID. It’s a clean way to undo a Git merge but isn’t suitable for all cases.

For the signs, a plus sign denotes whether a line is an addition to the working copy but not in that specific side of the merge, and a minus sign denotes whether the line is removed.

git merge -s octopus <branch1> <branch2>

Get started with a free trial of our Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.

As with a fast-forward merge, you won’t normally need to specify a recursive merge. However, you can make sure Git doesn’t choose something like a fast-forward merge using the following commands and flags:

git merge --no-ff

git merge -s recursive <branch1> <branch2>

On the whole, you use Git merge for many conflicts. However, rebasing has lots of benefits too. For example, while merging is simple to use and lets you preserve the context surrounding your merge history, rebasing can be cleaner as you can simplify your commit history into one.

This is because Git looks at those lines and deems the negative space to be a change.

When it’s time to merge, Git will recurse over the branch in order to make its definitive commit. This means a merge commit will have two parents once you complete it.

The second line uses the -s strategy option and explicit naming to carry out a merge. Unlike a fast-forward merge, a recursive merge does create a dedicated merge commit. For two-way merges, a recursive strategy is solid and works well.

Ours and Theirs

Logging is crucial for almost every piece of software that passes data. For Git, you can use the log to ascertain more detail on a merge conflict. You can access this information using git log:

A Terminal window showing the Git log for a repository. There are two commits with yellow titling, along with details on the author, date, and commit message.
Running and viewing the Git log in the Terminal.

While you can reverse more complex merge conflicts and the resultant changes, it can be tough because commits are often permanent.

As such, there are a lot of steps you need to follow:

  • First, you need to review commits and find references to the merges you need.
  • Next, checkout branches to review commit histories.
  • Once you have knowledge of the branches and commits you need, there are specific Git commands based on your desired action.

While a Git merge can be straightforward, there are often many other times when you need an advanced approach. This will see you use strategies such as recursive merging, three-way merges, and many more. You may even need to undo a Git merge at some point.