Merging
14.1 What Is a Merge in Git?
A merge is the process of combining the latest commits from one branch into another.
Examples:
• Merging a feature branch into main
• Merging bugfix work into develop
Git tries to auto-combine file changes. If it cannot, it asks you to resolve conflicts manually.
Examples:
• Merging a feature branch into main
• Merging bugfix work into develop
Git tries to auto-combine file changes. If it cannot, it asks you to resolve conflicts manually.
14.2 Types of Merges
Git supports several merge behaviors depending on the commit history.
Fast-Forward Merge
Happens when the target branch has no new commits since branching.
main -----> feature
Merging just moves the pointer:
3-Way Merge (Non-Fast Forward)
Occurs when both branches have diverged (i.e., both have unique commits).
Git creates a new merge commit linking the two histories:Result:
Squash Merge
Combines all commits from a branch into a single commit.Useful for:
• Cleaning up messy commit history
• Feature branches with many WIP commits
No-Fast-Forward Merge
Forces Git to create a merge commit even if a fast-forward is possible.Ensures feature branches appear as distinct history blocks.
Fast-Forward Merge
Happens when the target branch has no new commits since branching.
main -----> feature
Merging just moves the pointer:
1git checkout main
2git merge feature # simply moves main pointer forward3-Way Merge (Non-Fast Forward)
Occurs when both branches have diverged (i.e., both have unique commits).
Git creates a new merge commit linking the two histories:
1git checkout main
2git merge feature1main: A — B — C — M
2 ↖ feature: D — ESquash Merge
Combines all commits from a branch into a single commit.
1git merge --squash feature• Cleaning up messy commit history
• Feature branches with many WIP commits
No-Fast-Forward Merge
Forces Git to create a merge commit even if a fast-forward is possible.
1git merge --no-ff feature14.3 Performing a Basic Merge
Example:
Git outputs either:
• "Fast-forward"
• "Merge made by the 'recursive' strategy"
• Or conflict information
1git checkout main
2git merge login-pageGit outputs either:
• "Fast-forward"
• "Merge made by the 'recursive' strategy"
• Or conflict information
14.4 Merge Conflicts
A merge conflict happens when Git cannot automatically reconcile changes.
Typical conflict markers:
To resolve:
1. Open conflict file
2. Edit manually
3. Mark resolved
Typical conflict markers:
1<<<<<<< HEAD
2your changes
3=======
4incoming branch changes
5>>>>>>> featureTo resolve:
1. Open conflict file
2. Edit manually
3. Mark resolved
1git add <file>
2git commit14.5 When Should You Merge?
Merge is appropriate when:
• You want to preserve the true commit history
• You prefer a non-linear but complete visualization of work
• Multiple developers contribute to the same code base
Use merge when you want clarity and visibility of all changes.
• You want to preserve the true commit history
• You prefer a non-linear but complete visualization of work
• Multiple developers contribute to the same code base
Use merge when you want clarity and visibility of all changes.
14.6 Best Practices for Merging
• Merge often to reduce conflicts
• Merge from main into your feature branch periodically
• Avoid long-lived branches if possible
• Name branches clearly before merging
• Prefer --no-ff for feature development (optional but common)
• Merge from main into your feature branch periodically
• Avoid long-lived branches if possible
• Name branches clearly before merging
• Prefer --no-ff for feature development (optional but common)
14.7 Key Takeaways
• Merging integrates changes between branches.
• Git supports fast-forward, no-ff, squash, and 3-way merges.
• Merge conflicts must be resolved manually.
• Merges preserve full commit history.
• Choose merging when team collaboration requires safer, non-destructive history management.
• Git supports fast-forward, no-ff, squash, and 3-way merges.
• Merge conflicts must be resolved manually.
• Merges preserve full commit history.
• Choose merging when team collaboration requires safer, non-destructive history management.