bashintermediate

Git Merge Strategies — No-FF, Squash, Ours

Understand different merge strategies and when to use no-fast-forward, squash merge, and ours/theirs.

bash
# --- Fast-forward merge (default when possible) ---
# Moves pointer forward, no merge commit
git checkout main
git merge feature/login

# --- No fast-forward (always create merge commit) ---
# Preserves branch history in the log
git merge --no-ff feature/login -m "Merge feature/login"

# --- Squash merge (combine all commits into one) ---
# Stages all changes but doesn't commit
git merge --squash feature/login
git commit -m "feat: add login system"
# Note: branch history is lost, shows as single commit

# --- Ours strategy (keep our version on conflicts) ---
git merge -s ours feature/old-branch
# Discards all their changes, records the merge

# --- Theirs strategy (via recursive) ---
git merge -X theirs feature/upstream
# Auto-resolves conflicts using their version

# --- Ours for specific files ---
git merge feature/login
# If conflict:
git checkout --ours -- src/config.ts    # keep ours
git checkout --theirs -- src/auth.ts    # keep theirs
git add .
git commit

# --- Abort a bad merge ---
git merge --abort

# --- Check if branches can be fast-forwarded ---
git merge-base --is-ancestor feature/login main && echo 'Yes' || echo 'No'

Use Cases

  • Choosing the right merge strategy for PRs
  • Preserving feature branch history
  • Resolving conflicts with ours/theirs shortcuts

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.