# Rebasing Basics

> Keep your history perfectly linear by moving commits to a new base.

Subject: Version Control | Lesson 22 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/git-rebase
Previous: [The Reflog](https://codewithanimation.in/git/git-reflog.md) | Next: [Interactive Rebase](https://codewithanimation.in/git/interactive-rebase.md)

## The git rebase command

`git rebase` is the process of moving or combining a sequence of commits to a new base commit.

Internally, Git accomplishes this by creating entirely new commits and applying them to the specified base, effectively rewriting the project history to make it linear.

## Why it matters

The primary reason for rebasing is to maintain a perfectly linear and clean project history without superfluous merge commits.

A clean history makes it significantly easier to navigate, understand when bugs were introduced, and ensures a clean fast-forward merge of feature branches back into the main branch.

## How it works

Rebasing changes the base of your branch from one commit to another.

It detaches your sequence of commits from their original base, creates brand new commits with identical content changes, and re-attaches (or 're-plays') them onto the tip of the new base branch.

## Common mistake: The Golden Rule of Rebasing

Never rebase public branches. This is the #1 common mistake.

Because rebasing creates new commits with new hashes, force-pushing a rebased public branch replaces the old shared commits. It makes the project history abruptly vanish for your team, causing massive synchronization conflicts.

## Tip: Base your changes on what everybody has done

Mental Model: 'I want to base my changes on what everybody else has already done.'

Use rebase strictly for local, un-pushed feature branches to clean up your messy work before integrating it into the shared codebase.

## Try it: Replay commits on a new base

Move the feature changes onto main and compare their original and rewritten commit identities.

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

## Check yourself

### Question 1

What is the primary purpose of the `git rebase` command?

- A. To create a merge commit that joins two divergent branches together.
- B. To permanently delete uncommitted changes in your working directory.
- C. To move or combine a sequence of commits to a new base commit, creating a linear project history.
- D. To upload your local commits to a remote repository.

Correct answer: C

`git rebase` alters the commit history by re-applying commits from one branch onto the tip of another, producing a perfectly linear sequence of commits without extra merge commits.

- Why not A: This is what `git merge` does, preserving diverging histories.
- Why not B: This describes commands like `git restore` or `git reset --hard`.
- Why not D: This is the function of the `git push` command.

### Question 2

Why is the 'Golden Rule of Rebasing' so critical to follow?

- A. Rebasing public branches replaces old commits with new ones, overwriting shared history and causing severe conflicts.
- B. Rebasing public branches automatically deletes all the code in the repository.
- C. Rebasing requires administrative access to the Git server.
- D. Git prevents you from rebasing any branch that has more than 10 commits.

Correct answer: A

Because rebasing creates entirely new commit hashes, doing so on a shared branch and force-pushing it will cause synchronization chaos for your teammates when they try to pull.

- Why not B: It rewrites history, but doesn't delete the code contents.
- Why not C: Rebasing is purely a local operation.
- Why not D: Git does not enforce any limit on the number of commits.

### Question 3

How does Git internally accomplish a rebase?

- A. It simply renames the existing commits without changing their commit IDs.
- B. It creates a new 3-way merge commit that links the two branches together.
- C. It detaches commits from their original base, creates new commits with the same changes, and applies them to the new base.
- D. It compresses all commits into a single zip file and attaches it to the main branch.

Correct answer: C

Even though a rebased branch looks identical in terms of file content, it is composed of entirely new commits with different cryptographic hashes because their parent references have been updated.

- Why not A: Git creates new commit IDs because the history has changed.
- Why not B: A 3-way merge is created by `git merge`, whereas `rebase` avoids them.
- Why not D: Git manages changes via commit graphs, not zip files.

## Remember this

- Rebase replays your commits onto a new base.
- It creates a perfectly linear history.
- Golden Rule: NEVER rebase public, shared branches.
- Rebased commits are entirely new commits with new hashes.
