JuniorGit

What is the difference between git merge and git rebase? When should you use each?

Both bring changes from one branch into another, but they record history in two different ways.

  • git merge creates a merge commit that joins the two branches. History stays as it happened, and you can see exactly when the branch split off and came back.
  • git rebase rewrites your branch's commits on top of the target branch. History becomes a straight line that is easier to read, but the commits get new hashes.
Update a feature branch from main
git switch feat/login
git fetch origin
git rebase origin/main   # straight history; only do this on a branch that is yours alone

In an interview, mentioning the trade-off (true history versus readable history) and the rule your team actually follows earns more points than the definitions alone.