What is Merging?
Merging is the first method to combine work from two different branches. Thegit merge command creates a special commit that has two unique parents, effectively saying:
“I want to include all the work from this parent over here AND this one over here, and the set of all their parents.”
Merge commits are unique because they have two parent commits instead of one, allowing Git to preserve the complete history of both branches.
How Merge Works
Creating a Merge Commit
When you merge one branch into another:- Git creates a new commit with two parents
- This commit points to both branch tips
- The current branch moves forward to this new commit
- The merged branch stays where it was
Visual Example
Before merge:git merge bugFix:
C4 has two parents: C3 and C2.
The Merging in Git Level
The introductory level teaches merge fundamentals:Level Goal
Create a divergent history and merge it back together:Expected Result
Merge Direction
Merging INTO the Current Branch
The commandgit merge <branch> always merges INTO the currently checked-out branch:
The branch you’re merging FROM stays unchanged. Only the current branch (HEAD) moves forward.
Completing the Merge
After mergingbugFix into main, you might want to do the reverse:
State after first merge:
git checkout bugFix; git merge main:
bugFix was an ancestor of main, Git simply moved bugFix forward. This is called a fast-forward merge.
Types of Merges
Three-Way Merge
When branches have diverged, Git creates a new merge commit:M has two parents: A and B.
Fast-Forward Merge
When one branch is directly ahead of another: Before:git checkout main; git merge feature:
main just moves forward.
Merge in the Visualization
Color Blending
Learn Git Branching uses colors to show merge relationships:- Each branch has a unique color
- Commits are colored by which branches contain them
- After merging, colors blend together
main(blue) +bugFix(orange) = merged commits appear purple
Color blending helps you understand which branches contributed to each commit.
Following the Arrows
After a merge, you can follow arrows from the merge commit back through ALL ancestors:C4, you can reach C3, C2, C1, and C0.
Merge Implementation
From the source code, merges create special commit structures:Practical Examples
Example 1: Feature Integration
Example 2: Bug Fix Integration
Merge vs. Rebase
Merging preserves the complete history of both branches:Advantages
- Preserves exact history
- Shows when branches were integrated
- Non-destructive operation
- Clear branch points
Disadvantages
- Creates extra merge commits
- History can become complex
- Commit graph less linear
- More commits to navigate
Common Merge Patterns
Feature Branch Workflow
Release Branch Merges
Best Practices
Merge Forward
Regularly merge main into your feature branches to stay updated.
Clean Working Directory
Ensure no uncommitted changes before merging.
Test Before Merging
Verify your branch works before merging into main.
Meaningful Messages
Write clear merge commit messages explaining what was integrated.
Understanding Merge Conflicts
While Learn Git Branching doesn’t simulate conflicts, in real Git:When Conflicts Occur
- Same file modified in both branches
- Same line changed differently
- File deleted in one branch, modified in other
Resolving Conflicts
Advanced Merge Options
Merge Without Fast-Forward
Force creation of merge commit even when fast-forward is possible:Squash Merge
Combine all branch commits into one:Merge Commit Messages
Merge commits typically include:- Source and destination branches
- Brief description of what was integrated
- Any notable changes or decisions
Verifying Merges
Check Branch Contains All Changes
After mergingbugFix into main, the main branch now contains:
- All commits from
main’s original history - All commits from
bugFix - All commits both branches shared (common ancestors)
Visual Verification
In Learn Git Branching, look for:- Merge commit with two parent arrows
- Current branch pointing to merge commit
- Color blending showing combined history
Common Mistakes
Related Concepts
- Branching - Creating separate lines of development
- Rebasing - Alternative to merging for linear history
- Commits - Understanding what merges combine
Next Steps
After mastering merge:- Learn rebasing for an alternative integration strategy
- Practice resolving complex branch structures
- Understand when to use merge vs. rebase
- Explore remote branch merging