~/hackweb.dev
Merging Branches
Quiz
...

Merging Branches

intermediate · updated Tue Sep 08 2026Contribute

Combine branches with fast-forward and three-way merges.

Merging Branches

Merging combines the history of two branches into one. Git picks the best strategy automatically.

Basic Merge

git switch main
git merge feature

This integrates feature into main.

Fast-Forward Merge

Happens when main has no new commits since feature was created:

main:    A → B
feature: A → B → C → D

After merge:
main:    A → B → C → D

Git simply moves the pointer forward — no extra commit needed.

Three-Way Merge

Used when both branches have new commits:

main:    A → B → E
feature: A → B → C → D

After merge:
main:    A → B → E ─┐
         feature: C → D → M (merge commit)

Creates a new merge commit M combining both histories.

No Fast-Forward

Force a merge commit even for fast-forward cases:

git merge --no-ff feature

Useful for preserving branch history in your log.

Best Practices

  • Merge frequently to avoid large conflicts
  • Use --no-ff for feature branches to keep history clear
  • Always run tests before merging into main
  • Review the diff before completing a merge

Common Mistakes

  • Merging into the wrong branch
  • Forgetting to commit or stash changes before merging
  • Not pulling latest main before merging
  • Leaving merge conflicts unresolved