Git

What are Git Merge Strategies & Options

Git merge strategies can be used for integrating the latest modifications/changes from one branch into another. They determine how Git combines the changes made in various branches and resolves any conflicts that may arise. Merge strategies provide different ways to handle these conflicts, such as manual resolution, favoring changes from one branch over another, or combining changes.

This tutorial will explain the Git merge strategies and options.

What are Git Merge Strategies and Options?

While performing a Git merge operation in Git, users can choose different strategies and options to customize the behavior of the merge operation. Some most commonly used merging strategies are listed below:

  • Fast-forward Merge Strategy
  • Ort Merge Strategy
  • Recursive Merge Strategy

Fast-forward Merge Strategy

It is the default strategy that is used when developers want to merge branches that have a linear commit history. It simply moves the branch pointer to the latest commit of the merged branch, resulting in a fast-forward merge.

Step 1: Navigate to Git Local Directory
First, execute the “cd” command along with the path of the Git local directory for navigating to it:

cd "C:\Users\user\Git\testproject"

Step 2: List all Branches
Next, list all branches of the stated repository by using the “git branch” command:

git branch

The below-stated output shows that all branches have been listed successfully:

Step 3: Switch to Feature Branch
Next, switch to the feature branch by running the “git checkout” command with the specified branch name:

git checkout feature3

It can be observed that the branch has been switched successfully:

Step 4: Generate and Modify the File
Run the “‘echo” command to create a file and modify it:

echo linuxhint>>file.txt

Step 5: Track File
Execute the “git add” command to track all changes from the working area to the staging area:

git add file.txt

Step 6: Commit Changes
Commit all changes with the help of the “git commit” command and set the message for commit using the “-m” option:

git commit -m "file created and added"

It can be observed that all the changes have been committed successfully:

Step 7: Navigate to Master Branch
Switch to the master branch by running the “git checkout” along with the branch name “master”:

git checkout master

You have switched to the “master” branch successfully:

Step 8: Merge with Feature Branch
Run the “git merge” command along with the “feature3” branch to merge it with the “master” branch:

git merge feature3

The below-stated output indicates that both branches have been merged with the help of the “Fast-forward” merge strategy:

Ort Merge Strategy

The “Ort” merge is a relatively new Git merge strategy that is significantly faster and addresses correctness problems that exist in the merge-recursive strategy. Because “ort” is the default merge strategy in the latest releases of Git, merge results are now more predictable and consistent between the local machine and GitHub. Utilize the command stated below to perform the Git merge strategy:

git merge --no-ff feature3

The resultant image shows that the default editor has been launched to make a commit message:

The merging operation through the “ort” strategy has been performed successfully:

Recursive Merge Strategy

The recursive merging strategy is the default strategy for merging branches with complex commit histories. It performs a three-way merge, considering the commonality of the branches being merged, and attempts to intelligently merge the changes from both branches. In Recursive merge, after switching branches and making some commits, there are some new original commits on the “master”. So, when it’s time to merge, git resources over the branch and create a new merge commit.

That’s all about Git merging strategies and options.

Conclusion

The Git merge strategies are used to integrate the latest modifications made in one branch into another. To do so, there are various Git merge strategies, including “fast-forward”, “ort”, “recursive” and others used for this purpose. This tutorial demonstrated the Git merge strategies and options.

About the author

Hafsa Javed