bashintermediate
Git Merge Strategies — No-FF, Squash, Ours
Understand different merge strategies and when to use no-fast-forward, squash merge, and ours/theirs.
bashPress ⌘/Ctrl + Shift + C to copy
# --- 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.
bashintermediate
Git Rebase Workflow Example
Step-by-step git rebase workflow to keep feature branches up to date with main branch cleanly.
Best for: Keeping feature branches linear with main
#git#rebase
bashbeginner
Git Stash Usage Examples
Save, list, apply, and manage uncommitted changes with git stash for quick context switching.
Best for: Switching branches with uncommitted work
#git#stash
bashintermediate
Resolve Git Merge Conflicts
Step-by-step guide to identify, resolve, and complete merge conflict resolution in Git.
Best for: Resolving conflicts during branch merges
#git#merge
bashintermediate
Git Patch Create and Apply Workflow
Generate and apply git patches to share changes via email or transfer between repositories.
Best for: Sharing changes without push access
#git#patch