SyntaxStudy
Sign Up
Git git merge: Fast-Forward and 3-Way Merge
Git Beginner 10 min read

git merge: Fast-Forward and 3-Way Merge

Merging combines the history of two branches. A fast-forward merge simply moves the branch pointer forward when there is no diverging history. A 3-way merge creates a new merge commit when both branches have new commits, using the common ancestor as the base.

Example
# ---- Fast-forward merge ----
# main:    A - B
# feature:         C - D
# (main has not moved since feature branched off)

git switch main
git merge feature/add-button
# Fast-forward: main pointer moves to D, no merge commit created.
# main: A - B - C - D

# ---- 3-way merge ----
# main:    A - B - E
# feature:     C - D
# (both branches have new commits)

git switch main
git merge feature/user-login
# Git creates a merge commit M:
# main: A - B - E - M
#           \       /
#            C - D

# Prevent fast-forward (always create a merge commit):
git merge --no-ff feature/add-button -m "Merge feature/add-button"

# See a graph of the merge:
git log --oneline --graph

# Abort a merge in progress:
git merge --abort