What is Interactive Rebase in Git: Explained Simply
git is a command that lets you edit, reorder, combine, or remove commits before applying them to your branch. It helps you clean up your commit history by choosing exactly how each commit should appear.How It Works
Interactive rebase works like editing a list of your recent changes (commits) in a text editor before finalizing them. Imagine you wrote several notes on sticky notes and then decide to rearrange, combine, or erase some before sticking them on a wall. Interactive rebase lets you do this with commits.
When you run git rebase -i, Git shows you a list of commits in reverse chronological order (most recent first). You can then tell Git to pick, squash (combine), edit, or drop commits. After you save and close the list, Git applies the commits in the new order or form you specified, rewriting history as if you made those changes that way from the start.
This process is powerful because it lets you make your commit history cleaner and easier to understand before sharing your work with others.
Example
This example shows how to reorder and squash commits using interactive rebase.
git log --oneline # Shows recent commits git rebase -i HEAD~3 # Opens editor with last 3 commits listed # In editor, change lines like this: # pick abc123 Fix typo # squash def456 Add more tests # pick ghi789 Update README # Save and close editor # Git combines commits and rewrites history
When to Use
Use interactive rebase when you want to tidy up your commit history before sharing your code. For example, if you made many small fixes or experiments, you can combine them into meaningful commits. It is also useful to reorder commits to tell a clearer story or to remove mistakes.
Developers often use interactive rebase before pushing to a shared repository to keep the project history clean and easy to follow. It helps teams review changes better and maintain a professional codebase.
Key Points
- Interactive rebase lets you edit commit history before finalizing it.
- You can reorder, combine (squash), edit, or remove commits.
- It rewrites history, so use it only on local or private branches.
- Helps create a clean, understandable commit log.