How to Merge Two Branches in Git: A Complete Guide
Merging branches is one of the most common — and most misunderstood — operations in Git. Whether you're combining a finished feature into your main codebase or pulling in updates from a teammate's work, understanding how Git handles merges puts you in control of your project history.
What Does "Merging" Actually Mean in Git?
When you merge two branches, Git combines the changes from both into a single branch. Think of branches as parallel timelines of your project. Merging brings those timelines back together.
Git doesn't just paste one branch's changes on top of another. It looks at three points: the common ancestor (where the two branches originally diverged), the tip of the first branch, and the tip of the second. This is called a three-way merge, and it's how Git figures out what changed, what's new, and what might conflict.
The Basic Merge Command
To merge one branch into another, the core process is:
- Switch to the branch you want to merge into — typically main or develop
- Run the merge command pointing at the branch you want to bring in