What are the differences between the rebase and merge commands when combining branches in Git?
sobes.tech AI
Answer from AI
Main differences between git merge and git rebase:
-
git merge combines two branches, creating a new merge commit that preserves the history of both branches. The branching history is maintained, which can lead to a more complex commit graph.
-
git rebase moves commits from one branch onto another, rewriting history. This results in a linear history without merge commits.
Example:
If you have a feature branch diverged from main, and new commits appear in main:
-
git merge mainin thefeaturebranch will create a merge commit, preserving both histories. -
git rebase mainwill reapply the commits offeatureon top of the latest commits inmain, as if they were created after them.
The choice depends on team preferences: merge preserves branch history, while rebase makes the history cleaner and linear.