Last Updated: January 3, 2026
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.
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.
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.
The power of interactive rebasing lies in the commands you can use within the interactive rebase editor. Here are the most common commands:
These commands allow you to effectively manage your commit history.
Let’s explore some common scenarios where interactive rebase can be a lifesaver.
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.
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.
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.
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:
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.
To make the most of interactive rebase, consider these best practices:
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.