# Interactive Rebase

> Rewrite your local history by squashing, dropping, and rewording commits before sharing them.

Subject: Version Control | Lesson 23 of 29 | 7 min read
Interactive lesson: https://codewithanimation.in/git/interactive-rebase
Previous: [Rebasing Basics](https://codewithanimation.in/git/git-rebase.md) | Next: [Rebase vs Merge](https://codewithanimation.in/git/rebase-vs-merge.md)

## The git rebase -i command

`git rebase -i` (or `--interactive`) begins an interactive rebasing session.

Instead of blindly moving a sequence of commits to a new base, it opens an editor allowing you to alter individual commits. It lets developers rewrite local history by removing, splitting, reordering, or combining an existing series of commits before they are finalized.

## Why it matters

Web developers often make dozens of small, messy 'WIP' commits while building a feature (e.g., 'fix typo', 'try again').

Before merging the feature branch into main, `git rebase -i` allows you to rewrite this history. You can squash tiny commits into a single cohesive feature commit, ensuring the project's public history remains clean, linear, and easy to review.

## How it works

When you run `git rebase -i <base_commit>`, Git opens your default text editor with a script of your commits listed in chronological order.

Each line starts with a command word (defaulting to `pick`). You edit this file by changing the commands (e.g., `squash` to meld a commit into the previous one, `drop` to delete it) and save the file.

Once saved, Git automatically executes the script top-to-bottom, applying the specified actions step-by-step.

## Common mistake: Rebasing Public History

The #1 mistake is rebasing commits that have already been pushed to a shared repository.

Because rebase creates entirely new commits with different hashes, doing this causes your local branch to diverge from the remote. If you force push, you will overwrite your team's history, causing massive conflicts for everyone else.

## Tip: The Time Machine Script

Mental Model: Think of interactive rebase as a 'Time Machine Text Editor.' You are given a script of the past, and you can edit the script before hitting play to rewrite reality.

Pro Tip: Frequently squash your small 'WIP' commits into one clean, descriptive feature commit right before opening a Pull Request.

## Try it: Edit a rebase todo list

Choose `pick`, `squash` or `drop` for sample commits, then inspect the rewritten history.

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

## Check yourself

### Question 1

What is the primary purpose of using `git rebase -i`?

- A. To automatically resolve merge conflicts between two branches.
- B. To recover lost commits from a deleted branch.
- C. To rewrite and clean up a sequence of local commits before sharing them.
- D. To synchronize your local repository with a remote repository.

Correct answer: C

Interactive rebasing gives you a text-editor interface to precisely modify, squash, drop, or reword your recent local commits to create a clean, logical history before pushing.

- Why not A: Rebase doesn't automatically resolve conflicts.
- Why not B: `git reflog` is used to recover lost commits.
- Why not D: `git fetch` or `git pull` are used for synchronization.

### Question 2

What happens if you change the command word from `pick` to `squash` in the interactive rebase editor?

- A. The commit is completely deleted from the Git history.
- B. The commit's changes are applied, but the rebase pauses to let you edit the files.
- C. The commit is merged into the commit immediately preceding it in the list.
- D. The commit's message is changed, but it remains a separate commit.

Correct answer: C

Squashing (`s`) melds the selected commit into the commit directly above it in the interactive script, allowing you to combine multiple small 'WIP' commits into one unified feature commit.

- Why not A: That would be the `drop` command.
- Why not B: That is the `edit` command.
- Why not D: That is the `reword` command.

### Question 3

What is the golden rule of rebasing, especially interactive rebasing?

- A. Always rebase the `main` branch onto your feature branch.
- B. Never rebase commits that have already been pushed to a public, shared repository.
- C. Never squash more than three commits at a time.
- D. Always delete the `.git/rebase-merge` folder manually after finishing.

Correct answer: B

Because rebase rewrites history by generating entirely new commit hashes, rebasing a public branch will cause it to diverge from what other developers have locally, leading to messy merge conflicts.

- Why not A: You typically rebase your feature branch onto `main`.
- Why not C: There is no limit to how many commits you can squash.
- Why not D: Git cleans up its own internal rebase folders automatically.

## Remember this

- Interactive rebase lets you rewrite local history using a text editor.
- Change 'pick' to 'squash' to combine commits.
- Change 'pick' to 'drop' to delete a commit entirely.
- Never interactively rebase commits that have been pushed to a shared remote.
