A Detailed Guide to Git Interactive Rebase (with Examples)
Interactive rebase is one of Git's most powerful tools for rewriting commit history, giving you precise control to modify, reorder, squash, split, or drop commits before pushing them to a shared repository. While a standard git rebase
is useful for integrating changes, the interactive mode (git rebase -i
) provides a "surgical" way to curate your branch's history, making it cleaner and more understandable for your team (GitHub Docs, SitePoint, 30 Seconds of Code).
When and Why Use Interactive Rebase?
Interactive rebase is ideal for: - Cleaning up a messy feature branch before merging. - Combining multiple "work in progress" (WIP) or typo-fix commits into a single, meaningful commit. - Reordering commits to make the history more logical. - Editing commit messages for clarity. - Splitting a large commit into smaller, focused commits. - Removing unnecessary or erroneous commits.
Important: Never rebase commits that have already been pushed to a shared or public branch, as this rewrites history and can cause conflicts for collaborators (GitHub Docs, Atlassian).
How to Start an Interactive Rebase
You initiate an interactive rebase by specifying the commit before the oldest commit you want to modify. You can do this by reference (e.g., HEAD~3
for the last three commits) or by SHA-1 hash (HackerNoon).
Example:
Suppose your commit history looks like this (from newest to oldest):
a3c5e1d Add payment integration
b4d2c3a Fix typo in product model
c1b2a3f WIP: product model
d4e5f6a Initial commit
To interactively rebase the last three commits, run:
git rebase -i HEAD~3
Or, using a SHA:
git rebase -i b4d2c3a
This opens your default editor with a list like:
pick a3c5e1d Add payment integration
pick b4d2c3a Fix typo in product model
pick c1b2a3f WIP: product model
Understanding the Interactive Rebase Editor
Each commit is listed with the command pick
by default. You can change this command to perform different actions:
pick
: Use the commit as is.reword
: Edit the commit message.edit
: Pause to amend the commit's content.squash
: Merge this commit into the previous one, combining their messages.fixup
: Like squash, but discard this commit's message.drop
: Remove the commit from history (GitHub Docs).
Example 1: Squashing Commits
Suppose you want to combine "Fix typo in product model" and "WIP: product model" into a single commit:
pick a3c5e1d Add payment integration
squash b4d2c3a Fix typo in product model
pick c1b2a3f WIP: product model
When you save and close the editor, Git will prompt you to combine the commit messages.
Example 2: Rewording a Commit
To edit a commit message, change pick
to reword
:
pick a3c5e1d Add payment integration
reword b4d2c3a Fix typo in product model
pick c1b2a3f WIP: product model
Git will pause and let you edit the message.
Example 3: Splitting a Commit
To split a commit, change pick
to edit
for that commit. When the rebase stops, use:
git reset HEAD^
This unstages the changes, allowing you to add and commit them in smaller pieces (RIP Tutorial).
Example 4: Dropping a Commit
To remove a commit entirely, change pick
to drop
:
pick a3c5e1d Add payment integration
drop b4d2c3a Fix typo in product model
pick c1b2a3f WIP: product model
Handling Conflicts and Continuing
If you encounter conflicts during the rebase, Git will pause and prompt you to resolve them. After resolving, use:
git add
git rebase --continue
You can abort the rebase at any time with:
git rebase --abort
Or, if you want to edit the todo list during a paused rebase:
git rebase -i --edit-todo
Best Practices
- Use interactive rebase on local, unpublished branches to clean up history before merging (GitHub Docs, Atlassian).
- Communicate with your team when rewriting history.
- Always double-check the commit range you're rebasing to avoid losing important work.
Summary:
Interactive rebase is an essential tool for any Git user who wants clean, logical, and readable commit history. It empowers you to shape your branch's narrative, making collaboration and code review far more efficient and pleasant (SitePoint, RIP Tutorial).