Last Updated: January 3, 2026
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:
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.
When you initiate a three-way merge using git merge, Git performs several steps to combine changes:
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 is an essential part of the three-way merge. It acts as a bridge between two branches, containing pointers to both parent commits.
A merge commit has two parent commits, which can be visualized like this:
In this structure:
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.
The three-way merge method offers several advantages over simpler merge methods, such as fast-forward merges:
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.
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:
<<<<<<<, =======, >>>>>>>) indicating the conflicting changes. You need to edit these sections to create a single coherent version of the code.git add <file> to mark the conflicts as resolved.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:
To make the most of three-way merges and minimize conflicts, consider these best practices:
git diff to review changes before merging. This allows you to identify potential conflicts early.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.