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.

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:
1git checkout main 2git merge feature # simply moves main pointer forward

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:
1git checkout main 2git merge feature
Result:
1main: A — B — C — M 2 ↖ feature: D — E

Squash Merge
Combines all commits from a branch into a single commit.
1git merge --squash feature
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.
1git merge --no-ff feature
Ensures feature branches appear as distinct history blocks.

14.3 Performing a Basic Merge

Example:
1git checkout main 2git merge login-page

Git 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:
1<<<<<<< HEAD 2your changes 3======= 4incoming branch changes 5>>>>>>> feature

To resolve:
1. Open conflict file
2. Edit manually
3. Mark resolved

1git add <file> 2git commit

14.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.

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)

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.