0
0
Gitdevops~5 mins

Merge strategies overview in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When working with Git, you often combine changes from different branches. Merge strategies decide how these changes come together. Choosing the right strategy helps keep your project history clear and avoids conflicts.
When you want to combine a feature branch into the main branch after finishing work
When you need to update your branch with the latest changes from another branch
When you want to keep a clean, linear history without extra merge commits
When you want to preserve the exact history of changes including merge points
When you want to resolve conflicts manually during merging
Commands
This command merges the 'feature-branch' into your current branch using the default merge strategy, which creates a merge commit if needed.
Terminal
git merge feature-branch
Expected OutputExpected
Updating 1a2b3c4..5d6e7f8 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
This forces Git to create a merge commit even if the merge could be done with a fast-forward, preserving the branch history.
Terminal
git merge --no-ff feature-branch
Expected OutputExpected
Merge made by the 'recursive' strategy. file.txt | 2 ++ 1 file changed, 2 insertions(+)
--no-ff - Create a merge commit even if fast-forward is possible
This merges changes from 'feature-branch' by combining all commits into one, but does not create a merge commit automatically. You must commit manually.
Terminal
git merge --squash feature-branch
Expected OutputExpected
Squash commit -- not updating HEAD Automatic merge went well; stopped before committing as requested
--squash - Combine all changes into one commit without creating a merge commit
If a merge causes conflicts and you want to stop and return to the state before merging, this command cancels the merge.
Terminal
git merge --abort
Expected OutputExpected
Merge aborted
--abort - Cancel the current merge and restore the previous state
Key Concept

If you remember nothing else from this pattern, remember: merge strategies control how Git combines branches and affect your project history.

Common Mistakes
Using 'git merge' without understanding fast-forward merges
It can create no merge commit, making it hard to see when branches were combined
Use 'git merge --no-ff' to always create a merge commit if you want clear branch history
Running 'git merge --squash' and expecting an automatic commit
Squash merges do not create a commit automatically, so changes remain unstaged
After 'git merge --squash', run 'git commit' to finalize the merge
Ignoring conflicts during merge and forcing commit
This can cause broken code or lost changes
Use 'git merge --abort' to cancel and fix conflicts before retrying
Summary
Use 'git merge' to combine branches with a default strategy that may fast-forward.
Use '--no-ff' to keep merge commits and preserve branch history.
Use '--squash' to combine all changes into one commit without a merge commit.
Use '--abort' to cancel a merge if conflicts occur.