AlgoMaster Logo

three way merge

Last Updated: January 3, 2026

5 min read

A three-way merge occurs when Git combines changes from two branches into a common ancestor, allowing for a more accurate integration of divergent changes.

At its core, a three-way merge involves three different points in the commit history:

  1. Base commit: The common ancestor of the two branches being merged.
  2. Current commit: The tip of the first branch (e.g., the branch you are currently on).
  3. Other commit: The tip of the second branch being merged in.

This approach allows Git to understand the context of each change, making it possible to apply modifications intelligently. The three-way merge is particularly useful when both branches have diverged from a shared history, containing unique changes that need to be reconciled.

How Git Performs a Three-Way Merge

When you initiate a three-way merge using git merge, Git performs several steps to combine changes:

  1. Identify the commits: Git finds the base, current, and other commits.
  2. Create a merge commit: Git generates a new commit that represents the merge.
  3. Apply changes: Git analyzes the differences between the base and current commits, as well as between the base and other commits. It then applies these changes to create the final merged state.

The internal structure of the repository also changes. The .git directory will contain a new commit object, linking back to both parents (the two branches being merged).

Here's how this process looks:

This command triggers the three-way merge process. If conflicts arise, Git will pause and let you resolve them before finalizing the merge commit.

The Merge Commit

The merge commit is an essential part of the three-way merge. It acts as a bridge between two branches, containing pointers to both parent commits.

Structure of a Merge Commit

A merge commit has two parent commits, which can be visualized like this:

In this structure:

  • A represents the base commit.
  • B is the current branch's latest commit.
  • C is the other branch's latest commit.
  • D is the merge commit that combines the changes.

This structure provides a clear history of how two branches evolved together, allowing for easier navigation and understanding of the project's development over time.

Advantages of a Three-Way Merge

The three-way merge method offers several advantages over simpler merge methods, such as fast-forward merges:

  1. Contextual Awareness: By considering the base commit, Git understands the context of changes, reducing the chances of introducing errors.
  2. Conflict Resolution: In cases of conflicting changes, Git helps identify which lines were modified in each branch, making it easier for developers to resolve conflicts.
  3. History Preservation: The merge commit preserves the history of both branches, allowing for better tracking of changes and easier rollbacks if necessary.

Real-World Scenario

Imagine a scenario where two developers are working on a project. Developer A works on the feature branch implementing a new feature, while Developer B works on the main branch fixing bugs.

If both make changes to the same file, a three-way merge will help Git understand how to combine their changes intelligently based on the last common state of the file.

Conflict Resolution in Three-Way Merges

While three-way merges are powerful, they can lead to conflicts, especially when both branches have made changes to the same lines of code. Here’s how you can handle this situation:

  1. Detect Conflicts: After initiating a merge, Git will alert you to any conflicts that need resolution.
  2. Open the Conflicted Files: The files will be marked in the working directory. You can open them to see the conflicted sections.
  3. Resolve the Conflicts: You will see markers (e.g., <<<<<<<, =======, >>>>>>>) indicating the conflicting changes. You need to edit these sections to create a single coherent version of the code.
  4. Mark as Resolved: After resolving the conflicts, you use git add <file> to mark the conflicts as resolved.
  5. Complete the Merge: Finally, use git commit to finalize the merge.

Here’s an example of how conflicts might appear in a file:

After resolving the conflict, the function might look like this:

Best Practices for Three-Way Merges

To make the most of three-way merges and minimize conflicts, consider these best practices:

  • Frequent Merges: Regularly merge branches to keep changes synchronized. This reduces the complexity of the final merge.
  • Clear Commit Messages: Use descriptive commit messages to clarify the purpose of each change. This aids in future merges.
  • Feature Branches: Implement feature branches for separate tasks. This keeps the main branch stable and reduces merge conflicts.
  • Review Changes: Use git diff to review changes before merging. This allows you to identify potential conflicts early.

Conclusion

Three-way merges are a powerful feature of Git that help developers reconcile changes from multiple branches while considering their shared history. Understanding how Git performs these merges, the structure of merge commits, and conflict resolution techniques is crucial for effective collaboration.

Now that you understand how three-way merges operate and their implications in collaborative workflows, you are ready to explore merge conflicts.

In the next chapter, we will delve into how to effectively identify and resolve conflicts that arise during merges, ensuring a smoother integration process for your projects.