How does the git rebase command work, and when should you use it instead of merging?

git rebase takes the commits from your current branch and replays them one by one on top of another branch's latest commit, producing a new, linear sequence of commits instead of merging two diverging histories together.

Key Points

Example

Before opening a pull request, a developer rebases their feature branch onto the latest main with git rebase main, resulting in a clean, linear set of commits that reviewers can read top to bottom without any merge noise.

git checkout feature-branch
git rebase main

Interview Tip

A concise interview answer is

Rebase replays your branch's commits on top of another branch, rewriting their hashes to produce a clean, linear history. I use it to update a feature branch with main before opening a pull request, but I avoid it on commits that are already pushed and shared, where merge is the safer option.

Last updated: 11 November 2026

Previous question: How would you handle a situation where a merge conflict occurs in a critical piece of code just before deployment?

Next question: How do you manage merge conflicts in Git?

More Git - Level II interview questions

All Git - Level II questions