Script Valley
Git and GitHub Complete Course: From Beginner to Advanced
Branching, Merging, and Resolving ConflictsLesson 2.3

Merging Branches

git merge, fast-forward merge, three-way merge, merge commit, --no-ff flag

Merging Branches

Merging integrates changes from one branch into another. It is the standard way to bring a completed feature back into the main branch. Git performs merges automatically when possible, and asks for your help when it cannot.

DiagramFast-Forward vs Three-Way Merge

IMAGE PROMPT (replace this block with your generated image):

Flat two-panel merge diagram on white background. Title: Git Merge: Fast-Forward vs Three-Way. Panel 1 (top): Fast-Forward Merge. Shows main branch: C1 โ†’ C2. Feature branch from C2: C2 โ†’ F1 โ†’ F2. Before merge: main badge at C2, feature badge at F2. After merge arrow: main badge slides forward to F2. No new commit created. Label: No merge commit โ€” pointer just moves forward. Panel 2 (bottom): Three-Way Merge. Shows main: C1 โ†’ C2 โ†’ C3 (new commit on main after branching). Feature branch from C2: C2 โ†’ F1 โ†’ F2. After merge: a new merge commit M1 appears with two parent arrows โ€” one from C3, one from F2. M1 has badge Merge commit (2 parents) in #3A5EFF. Common ancestor C2 labeled with a dashed circle. Flag: git merge --no-ff always creates merge commit even when fast-forward is possible. Brand color #3A5EFF for merge commit. White background.

Fast-Forward Merge

If the target branch has not diverged from the source branch โ€” meaning no new commits were made on the target after branching โ€” Git performs a fast-forward merge. It simply moves the target branch pointer forward to the source branch tip. No merge commit is created.

git switch main
git merge feature/user-login

Three-Way Merge

When both branches have new commits since they diverged, Git uses a three-way merge. It finds the common ancestor commit, compares both branches against it, and creates a new merge commit that combines the changes. This merge commit has two parents.

Forcing a Merge Commit

Even when a fast-forward is possible, teams often prefer to always create a merge commit to preserve branch history:

git merge --no-ff feature/user-login -m "Merge feature/user-login into main"

The --no-ff flag ensures a merge commit is always created. This makes the branch topology visible in git log --graph.

Viewing the Merge

After merging, run git log --oneline --graph to see the branching and merging structure visually in your terminal.

Up next

Resolving Merge Conflicts

Sign in to track progress

Merging Branches โ€” Branching, Merging, and Resolving Conflicts โ€” Git and GitHub Complete Course: From Beginner to Advanced โ€” Script Valley โ€” Script Valley