Merge Strategies and Options
You'll learn how Git chooses different algorithms to combine branches and how to control merge behavior with specific options.
What Are Merge Strategies?
When you merge branches, Git doesn't always do it the same way. A merge strategy is the algorithm Git uses to combine changes from different branches. Think of it like choosing between different recipes to mix ingredients—some recipes work better for simple mixtures, others for complex ones.
Common Merge Strategies
Recursive is Git's default strategy for merging two branches. It intelligently handles complex histories, including cases where branches have diverged significantly. This is what you've been using implicitly in previous lessons.
Octopus is used when merging three or more branches simultaneously. It's useful for bringing multiple feature branches together at once, but it refuses to handle conflicts—it only works when merges are clean.
Key Merge Options
While strategies determine how Git merges, options control specific behaviors:
--no-ff (no fast-forward) forces Git to create a merge commit even when a fast-forward is possible. This preserves the visual history that a branch existed. Without it, fast-forwards make the branch disappear from history as if changes were made directly on the main branch.
git merge --no-ff feature-branch
This creates a dedicated merge commit, making it clear in the log that "feature-branch" was merged, even if it could have been fast-forwarded.
Why this matters: Using --no-ff on important branches (like merging features into main) keeps your history readable and makes it easier to understand when and why changes were introduced.
Key Takeaway: Git's merge strategies (like recursive and octopus) determine how branches combine, while options like --no-ff give you control over preserving branch history in your commit log.