TechnicalMedium Git
What is the difference between git merge and git rebase?
Asked for Frontend Developer, Backend Developer, Full-Stack Developer, Mobile Developer
Why they ask this: Every team uses Git; this checks you can collaborate without rewriting shared history.
Answer
Both bring changes from one branch into another:
git merge maincreates a merge commit that joins the two histories. Nothing is rewritten, so it is safe on shared branches, but the history shows every merge.git rebase mainreplays your branch's commits on top ofmain, creating new commits with new hashes. The result is a clean, linear history.
bash
git switch feature/login
git fetch origin
git rebase origin/main # update your feature branch
# fix conflicts, then: git add <file> && git rebase --continue
git push --force-with-lease # needed because history changedGolden rule: never rebase commits that other people have already pulled (such as main). Rebase your own feature branch; merge into shared branches, usually through a pull request.
Can you explain this in your own words?