Interview Q&A / Git & GitHub

IntermediateIn-depth3 min readUpdated 2 Oct 2026

What is the difference between git merge and git rebase?

SHORT ANSWER

Both bring changes from one branch into another. Merge joins the two histories with a new merge commit and keeps everything as it happened. Rebase replays your commits on top of the other branch, giving a straight, linear history but rewriting commit IDs. Never rebase commits that others have already pulled.

Explain it simply

Say you made a feature/login branch from main on Monday. By Wednesday, your teammates have added new commits to main. You now need their changes in your branch.

  • Merge says: "Take both lines of work and tie them together with a knot." Git creates a new merge commit with two parents. Nothing old is changed.
  • Rebase says: "Pretend I started my work today." Git lifts your commits off, moves your branch to the latest main, and replays your commits one by one on top. The history looks like a straight line.

Merge is honest about what happened. Rebase is tidier to read.

Example

Starting point:

text
          A---B   feature/login
         /
    C---D---E---F   main

Merge main into the feature branch:

bash
git switch feature/login
git merge main
text
          A---B---M   feature/login
         /       /
    C---D---E---F   main

M is the merge commit.

Rebase the feature branch onto main:

bash
git switch feature/login
git rebase main
text
                  A'--B'   feature/login
                 /
    C---D---E---F   main

A' and B' contain the same changes as A and B, but they are new commits with new IDs.

How it works

Merge finds the common ancestor (D), combines the changes from both sides, and records the result in one commit. If there are conflicts, you fix them once.

Rebase saves your commits as patches, resets your branch to the tip of main, then applies each patch in order. If there are conflicts, you may fix them once per commit, running git rebase --continue after each. git rebase --abort takes you back to where you started.

Real-world use

  • Many teams rebase their own local feature branch on the latest main before opening a pull request, so the review shows a clean set of commits.
  • git pull --rebase avoids the extra "Merge branch 'main' of ..." commits that clutter history.
  • Teams that want to see exactly when a feature landed prefer merge commits on main.
  • GitHub's "Squash and merge" and "Rebase and merge" buttons are just these ideas applied at the end of a pull request.

Common mistakes

  • Rebasing a shared branch. This is the golden rule: do not rebase commits that you have already pushed and someone else may have pulled. Their copy still has the old commit IDs, and the histories split into duplicates and confusion.
  • Using git push --force after a rebase without checking. Use git push --force-with-lease, which refuses to push if someone else updated the remote branch in the meantime.
  • Panicking in the middle of a rebase. Run git status; it tells you exactly which step you are on and what to do.

What to say in the interview

"Both integrate changes from one branch into another. Merge creates a merge commit with two parents and preserves the true history, so it is safe on shared branches. Rebase replays my commits on top of the target branch, which gives a linear history but creates new commit hashes. The golden rule is never rebase public history that others have pulled. In practice I rebase my local feature branch onto main to keep it clean, and merge into main through a pull request."

Follow-up questions

  • What is a fast-forward merge?
  • What does git rebase -i do?
  • Why is --force-with-lease safer than --force?
  • How would you recover if a rebase went wrong?

Go deeper

Read these once the basics above feel easy.

Interactive rebase lets you clean up your own commits before sharing them:

bash
git rebase -i HEAD~3

Git opens an editor listing the last three commits. Change pick to squash (or s) to fold a commit into the one above it, reword to edit a message, or drop to remove it. This is how you turn "fix", "fix again" and "final fix" into one meaningful commit.

#git#merge#rebase#branching

Related questions

Practise more Git & GitHub questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Git & GitHub quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.