Explain the difference between git merge and git rebase.?
Both commands integrate changes from one branch into another, but they work differently:
- git merge combines the histories of two branches, creating a new “merge commit.”
- git rebase rewrites history by placing your branch’s commits on top of another
branch, making it look like you developed your changes sequentially.
Real-world analogy:
- Merge: Like combining two storylines into one — you keep both histories.
Follow:
- Rebase: Like rewriting your story so it appears you followed the main storyline all
along.
Example:
If your feature branch has diverged from main, merging will keep both timelines, while
rebasing will make it look like your branch was based on the latest main version all along.