Middle
What is the difference between rebase and merge in Git?
sobes.tech AI
Answer from AI
Merge creates a new merge commit, combining the branch histories. Rebase moves your branch commits on top of another, rewriting history.
When to use:
- Merge: Preferable for merging feature branches into the main branch, preserves history.
- Rebase: Good for cleaning up local history before pushing, makes history linear.
Comparison:
| Characteristic | Merge | Rebase |
|---|---|---|
| Operation type | Creates a merge commit | Rewrites history by moving commits |
| History | Preserves branch structure, can be complex | Linear, but can be hard to follow |
| Collaboration | Safer when working in a team | Can cause issues when working with shared branches if history is already published |
# Example merge
git checkout main
git merge feature-branch
# Example rebase
git checkout feature-branch
git rebase main
git checkout main
git merge feature-branch // Fast-forward merge