AlgoMaster Logo

Interactive Rebase

Last Updated: January 3, 2026

5 min read

Interactive rebasing is one of the most powerful features of Git, allowing you to refine your commit history in an intuitive way. It offers a chance to clean up your project history by enabling you to edit, squash, reorder, and even drop commits.

Understanding how to use interactive rebase effectively can help you maintain a clean, understandable commit history, which is vital for team collaboration and project maintenance.

What is Interactive Rebase?

At its core, git rebase -i (the command for interactive rebase) allows you to step through a series of commits, making choices about what to do with each one. Instead of simply moving your commits from one branch to another, interactive rebase gives you control over the history.

When you run this command, Git opens an editor with a list of commits that you can modify. Each commit is prefixed with a command, usually pick, that tells Git what to do with that commit. This flexibility is invaluable when you need to clean up your commit history before merging into a main branch.

Starting an Interactive Rebase

To start an interactive rebase, use the following command, replacing N with the number of commits you want to rebase:

For example, if you want to edit the last three commits, you would run:

After executing this command, your default text editor opens with a list of the last three commits, looking something like this:

The commits are listed in the order they were made, with the oldest at the top.

Commands in Interactive Rebase

The power of interactive rebasing lies in the commands you can use within the interactive rebase editor. Here are the most common commands:

  • pick: Use the commit.
  • reword: Use the commit but edit the commit message.
  • edit: Pause the rebase after applying the commit, allowing you to amend it.
  • squash: Combine this commit with the previous commit.
  • fixup: Like squash, but discard this commit's message.
  • drop: Remove the commit entirely.

These commands allow you to effectively manage your commit history.

Practical Examples of Interactive Rebase

Let’s explore some common scenarios where interactive rebase can be a lifesaver.

Squashing Commits

Imagine you have several small commits that address a single feature or fix. Instead of cluttering your history, you can squash them into one meaningful commit.

For instance, suppose your rebase list looks like this:

You can change it to:

When you save and close the editor, Git will combine these commits into one. You will then be prompted to edit the commit message for the new combined commit.

Reordering Commits

Sometimes, the order of commits matters. Perhaps you have a commit that adds functionality, and another that updates tests based on that functionality. You can rearrange them in the interactive rebase list:

Change it to:

After saving, Git applies the commits in the new order.

Editing Commits

If you realize that a commit wasn’t correct, you can edit it. For example:

When you proceed, Git will pause at the commit you chose to edit, allowing you to make changes. You would run:

After amending your changes, you can continue the rebase:

This flexibility is powerful for maintaining a clean history.

Handling Conflicts During Interactive Rebase

Conflicts can arise during an interactive rebase, especially if you are squashing commits or reordering them. When you encounter a conflict, Git will pause the rebase and inform you about the files that have conflicts.

To resolve this:

  1. Open the files with conflicts and resolve them.
  2. Stage the resolved files:

Continue the rebase:

If you want to abort the rebase at any point, you can run:

This command returns your branch to the state it was in before the rebase began, ensuring that you can recover easily from mistakes.

Best Practices for Interactive Rebase

To make the most of interactive rebase, consider these best practices:

  • Use interactive rebase before merging: Always clean up your commits before merging into the main branch. This makes your project's history easier to read.
  • Work on feature branches: Keep your work on separate branches. This way, you can perform rebases without affecting the main branch until you're ready.
  • Communicate with your team: If you are working in a collaborative environment, ensure everyone is aligned on the commit history strategy. This prevents confusion with multiple developers pushing rebased commits.

By following these strategies, you can leverage interactive rebase to maintain a clean and comprehensible project history.

Now that you understand the ins and outs of interactive rebase, you are ready to explore the differences between rebasing and merging.

In the next chapter, we will look at how each approach affects your project's history and collaboration dynamics, helping you make informed decisions about which method to use in different scenarios.