LearnThatStack Ace your next interview
Git & Version Control · question
Question 5 of 54

When should you use git merge versus git rebase to combine branches?

beginner
← All Git & Version Control questions
Re-explain
Visual ↓

Use merge on shared branches. Use rebase to tidy your own branch before others pull it.

git merge records the join in a new commit and rewrites nothing, so merge is safe everywhere. The cost is a busier history, because every merge of diverged branches adds a merge commit.

git rebase replays your commits onto the other branch. The copies get new hashes, so they are new commits. Anyone who already had the old ones now holds a diverged branch, which is why you never rebase shared history.

Concern git merge git rebase
History branch and join one straight line
Existing commits kept as they are replaced by copies with new hashes
Conflicts fixed once can repeat per replayed commit

A common workflow uses both. Rebase the feature branch onto the newest main, then merge it:

git switch feature
git rebase main     # replay my commits on the newest main
git switch main
git merge feature   # publish the finished work
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Git & Version Control One question at a time. Answer out loud, get graded, see what you missed.
See it - interactive diagram
Merge keeps commits, rebase copies them
  • main
  • team/search
  • feature
initial commitTwo branches are about to start from this commit on main.
add search APITeammates have already pulled this branch, so its commits are shared history.
add search teststeam/search
add login formThis branch is yours alone. You have not pushed it, so nobody else has these commits.
validate login
fix typoMain moves on with a commit that neither branch has yet.
merge team/searchmainMerge adds one commit with two parents, one from each branch. The shared commits keep their hashes.
add login formRebase replays your commit on top of the newest main. The copy gets a new hash, so it is a new commit.
validate loginHEAD -> featureThe originals are left with no branch. If a teammate had pulled them, their history would no longer match yours.

Merge adds one commit and leaves the old ones alone. Rebase swaps your commits for copies with new hashes, so rebase only a branch nobody else has.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

What you can say

  1. I merge on shared branches, and I rebase only my own branch, to tidy it up before anyone else pulls it.
  2. Merge records the join in a new commit and rewrites nothing, so it's safe anywhere, but every merge of diverged branches adds a merge commit and the history gets busier.
  3. Rebase replays my commits on top of the other branch, and the copies get new hashes, so they're new commits.
  4. That's why I never rebase shared history, because anyone who already has the old commits ends up with a diverged branch.
  5. In practice I use both, so I rebase my feature branch onto the newest main and then merge it into main to publish it.
  6. The cost of rebasing is that conflicts can come back once for every replayed commit, where a merge has me fix them once.

Weak answers to avoid

  • Says always rebase because history looks cleaner Skips the cost. Rebase makes new commits with new hashes, so it is safe only on a branch nobody else has. Shared branches get merged.
  • Thinks rebase edits commits in place Rebase writes copies with new hashes and leaves the old commits behind. Anyone who already has the old ones now has a diverged branch, and that is the risk.
  • Treats merge and rebase as either-or A common workflow uses both. Rebase the feature onto the newest main, then merge it. Saying that shows you've used them on a real team.
  • Describes the commands but never says when The question asks when. Open with the rule, merge shared branches and rebase only your own, then back it up with what each command does to commits.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Git & Version Control cheatsheet.

← Back to all Git & Version Control questions
Pro · $10/mo

47 of 54 Git & Version Control answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime