CurriculumBranching & MergingMerging Branches
Lesson 3 of 560%
Beginner9 min+90 XP

Merging Branches

Merging integrates changes from one branch into another. Git automatically finds the common ancestor and combines changes. There are two main strategies: fast-forward (linear) and three-way merge (creates a merge commit).

01.Fast-Forward Merge

When the target branch (main) has no new commits since the feature branch was created, Git can simply move the pointer forward — no merge commit is needed. The result is a perfectly linear history. This is the default when possible and keeps history clean and readable.

Branch Visualization
featuremainHEAD
main
feature
HEAD

02.Three-Way Merge

When both branches have new commits since they diverged, Git performs a three-way merge using: the tip of your current branch, the tip of the incoming branch, and their common ancestor commit. Git creates a new "merge commit" with two parents. Use --no-ff to always force a merge commit even when fast-forward is possible.

Commit Timeline
a1b2c3d
init
b2c3d4e
feat
c3d4e5f
fix
d4e5f6a
HEAD

Commands

commands — click to reveal output
$
git checkout main
First: switch to the branch you are merging INTO
$
git merge feature/login
Merge feature/login into current branch
▼ show output
$
git merge --no-ff feature/signup
Always create a merge commit (preserves branch history in log)
▼ show output
$
git merge --squash feature/wip
Squash all branch commits into one staged change to commit yourself
$
git merge --abort
Abort a merge that has conflicts (returns to pre-merge state)
$
git log --oneline --graph
Visualize merged history
▼ show output
KNOWLEDGE CHECK

When does a fast-forward merge happen?

Rate this lesson:
PreviousNext Lesson