Merging
Combine branches and integrate work
Skills taught: git.merge, git.fast-forward, git.three-way-merge
Fast-forward merge
When main hasn't changed since you branched, Git simply moves the pointer forward. No merge commit is created.
# Situation: main has no new commits since feature branched
# main: A---B
# feature: C---D
git switch main
git merge feature/dark-mode
# → Fast-forward
# → main pointer moves to D
# Result:
# main: A---B---C---D (same as feature)
Three-way merge
When both branches have diverged (both have new commits), Git creates a merge commit that has two parents.
# Situation: both branches have new commits
# main: A---B---E
# feature: \---C---D
git switch main
git merge feature/dark-mode
# → Merge made by the 'ort' strategy
# → Creates merge commit M
# Result:
# main: A---B---E---M (M has parents E and D)
# \---C---D--/
Common merge options
# Merge a branch
git merge feature/login
# Always create a merge commit (even if fast-forward is possible)
git merge --no-ff feature/login
# Squash all commits into one (then you commit manually)
git merge --squash feature/login
git commit -m "Add login feature"
# Abort a merge in progress (if you change your mind)
git merge --abort
# After resolving conflicts, complete the merge
git add .
git merge --continue
Frequently Asked Questions
Should I use merge or rebase?
Use merge for integrating feature branches into main — it preserves the full history. Use rebase to update a feature branch with the latest main (keeping a linear history). Never rebase shared branches.
What is --no-ff and when do I need it?
--no-ff (no fast-forward) forces Git to always create a merge commit even when a fast-forward is possible. This preserves the branch history so you can see that a feature branch existed. Many teams require this for main.