Middle+
What to choose: merge or rebase in Git? What is the difference and when to use rebase?
sobes.tech AI
Answer from AI
Difference between merge and rebase:
- merge combines branches, creating a new merge commit. The history retains all branches and their divergence points.
- rebase moves commits from one branch onto another, rewriting history to make it linear.
When to use rebase:
- To maintain a clean and linear commit history.
- When updating your branch with the latest changes from the main branch before merging.
Example:
# Update feature branch from master using rebase
git checkout feature
git fetch origin
git rebase origin/master
This helps avoid unnecessary merge commits and simplifies history, but requires caution when working with public branches to avoid rewriting shared history.