# Rebase vs Merge

> Understanding the difference between a clean linear history and a complete contextual history.

Subject: Version Control | Lesson 24 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/rebase-vs-merge
Previous: [Interactive Rebase](https://codewithanimation.in/git/interactive-rebase.md) | Next: [Pull Requests](https://codewithanimation.in/git/pull-requests.md)

## Merge vs Rebase

**Merge**: A non-destructive operation that takes the contents of a source branch and integrates them into a target branch by creating a single new 'merge commit.'

**Rebase**: A history-rewriting operation that moves the entire feature branch to begin on the tip of the main branch. Instead of a merge commit, it creates brand new commits for each original commit.

## Why it matters

Choosing between rebase and merge dictates a team's collaboration convention.

**Merge** prioritizes a Complete History: It shows exactly when and how features were integrated, but can pollute the history with extra merge commits.

**Rebase** prioritizes a Clean History: It eliminates unnecessary merge commits, creating a pristine timeline that simplifies debugging (e.g., using `git bisect`).

## How it works

**Merge** integrates branches by creating a new commit that ties together the histories of both branches. Visually, this creates a diamond shape in the commit graph.

**Rebase** detaches the feature branch commits from their original starting point and re-applies them one-by-one onto the latest commit of the target branch. Visually, this results in a perfectly linear line.

## Common mistake: The Catastrophe of Rebasing Public History

The #1 common mistake is rebasing a public branch.

When developers rebase a branch that others have already pulled, it rewrites the commit history locally. When pushed, Git sees this as a diverged history, leading to massive confusion, duplicate commits, and synchronization nightmares for the rest of the team.

## Tip: The Golden Rule of Rebasing

Mental Model: *Never use rebase on public branches.*

Before typing `git rebase`, always ask yourself: 'Is anyone else looking at this branch?' If the answer is yes, take your hands off the keyboard and use `git merge` instead. Only rebase private, local branches.

## Try it: Compare merging and rebasing

Select an integration method and follow how it changes the shape of the history.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/rebase-vs-merge

## Check yourself

### Question 1

What is the defining characteristic of a `git merge` operation compared to `git rebase`?

- A. It rewrites the commit history to be perfectly linear.
- B. It is a non-destructive operation that creates a new commit tying both histories together.
- C. It is only used to clean up local, private commits before a pull request.
- D. It automatically resolves all code conflicts without developer intervention.

Correct answer: B

Merge preserves history and adds a merge commit, avoiding history rewrites. Visually, it creates a diamond shape in the graph.

- Why not A: This describes rebasing.
- Why not C: Merging is widely used to integrate branches publicly.
- Why not D: Both merge and rebase require manual intervention for conflicts.

### Question 2

Why do many development teams prefer rebasing over merging for local feature branches?

- A. It preserves the exact timestamp of when upstream changes were incorporated.
- B. It provides a perfectly linear project history, eliminating extraneous merge commits.
- C. It allows developers to safely rewrite the history of the shared `main` branch.
- D. It is the only way to push changes to a remote repository.

Correct answer: B

A perfectly linear history makes it much easier to navigate and read with tools like `git log` and `git bisect`.

- Why not A: Rebasing actually loses this context; merging preserves it.
- Why not C: Rewriting the `main` branch violates the Golden Rule of Rebasing.
- Why not D: `git push` works regardless of whether you merged or rebased.

### Question 3

According to the 'Golden Rule of Rebasing', when should you NEVER use `git rebase`?

- A. When your local feature branch has more than 5 commits.
- B. When you are trying to squash commits together.
- C. When your target branch is `main`.
- D. On a public branch that other developers are actively using.

Correct answer: D

Rewriting public history causes everyone else's local repositories to diverge from the remote, causing massive confusion and conflict resolution headaches.

- Why not A: You can rebase any number of local commits.
- Why not B: Interactive rebasing is exactly what you should use to squash commits locally.
- Why not C: You often rebase your feature branch ONTO `main`.

## Remember this

- Merge creates a non-destructive merge commit (diamond shape).
- Rebase rewrites history to create a linear path.
- Merge preserves complete context; rebase prioritizes clean history.
- Never rebase a public branch that others are using.
