Git & GitHub Interview Question #45

What is the difference between merge and rebase?

Remotes, Fetch, Pull, Push, Merge, Conflicts, Rebase & Stash Mid-Level Intermediate

Quick Interview Answer

Merge combines histories while preserving their existing branch structure and may create a merge commit. Rebase reapplies commits onto a new base and rewrites those commit identities, often producing a linear history.

Detailed Explanation

Merge combines histories while preserving their existing branch structure and may create a merge commit. Rebase reapplies commits onto a new base and rewrites those commit identities, often producing a linear history.

Neither approach is universally superior. Merge is straightforward for preserving shared history, while rebase can make a private feature branch easier to read before integration. Teams should use a consistent policy.

Code Example

# Merge approach
git switch main
git merge feature/search

# Rebase approach
git switch feature/search
git rebase main