Junior — Middle
Explain the difference between 'git rebase' and 'git merge' commands in Git branch management.
sobes.tech AI
Answer from AI
The git merge and git rebase commands are used to integrate changes from one branch into another, but they do so in different ways.
-
git merge creates a new merge commit that combines the histories of both branches, preserving their parallel development. The branch history remains branched.
-
git rebase re-applies (rewrites) the commits of the current branch on top of another branch, creating a linear history without merge commits.
Example:
# merge
git checkout feature
git merge main # creates a merge commit
# rebase
git checkout feature
git rebase main # "reapplies" feature commits on top of main
Using rebase simplifies the history but rewrites commits, which can be dangerous for public branches. merge is safer for collaborative work as it preserves history without rewriting.