git merge
Purpose
git merge is used to combine changes from one branch into another branch.
It integrates the history of two branches.
Unlike rebase, merge does NOT rewrite history.
Basic Syntax
git merge branch-name
Example:
git switch main
git merge feature-branch
This merges feature-branch into main.
What Happens Internally
Git finds the common ancestor between branches and combines changes.
There are two main types of merge:
-
Fast-forward merge
-
Three-way merge (creates merge commit)
1. Fast-Forward Merge
Before:
main: A — B
feature: A — B — C
If you merge feature into main:
git switch main
git merge feature
After:
main: A — B — C
- No merge commit created.
- History remains linear.
2. Three-Way Merge (Non Fast-Forward)
Before:
main: A — B — C
feature: A — B — D — E
After:
git switch main
git merge feature
Result:
A — B — C ——– M \ / D — E
Git creates a merge commit (M).
This preserves branch history.
Force Merge Commit (Even If Fast-Forward)
git merge –no-ff feature-branch
Creates a merge commit even when fast-forward is possible.
Used when:
- You want clear branch history
- You want feature tracking
Handling Merge Conflicts
If conflict occurs:
- Git marks conflict in files
- Manually resolve conflicts
-
Stage resolved files:
git add .
-
Complete merge:
git commit
To abort merge:
git merge –abort
Common Real-World Backend Use Cases
- Merging feature branch into main
- Integrating release branch
- Combining hotfix branch into production
- Team collaboration workflow
Merge vs Rebase
Merge:
- Preserves complete history
- Safe for shared branches
- Creates merge commits
- No history rewriting
Rebase:
- Rewrites history
- Cleaner linear history
- Dangerous if used on public branches
Rule of thumb:
Use merge for shared branches. Use rebase for local feature cleanup.
Common Mistakes
- Merging without pulling latest main
- Resolving conflicts incorrectly
- Merging large outdated branches
Best Practice in Enterprise Projects
Before merging:
git fetch
git switch feature-branch
git rebase origin/main
Then:
git switch main
git merge feature-branch
This reduces merge conflicts and keeps history clean.
Summary
git merge combines branches.
Types:
- Fast-forward merge
- Three-way merge (merge commit)
Safe for team environments.
Does not rewrite history.