
Module 6 Lesson 4: Interactive rebase
Master the 'Refactor' for your history. Learn how to use 'git rebase -i' to squash commits, reorder history, and edit old commit messages to make your project look perfect.
Module 6 Lesson 4: Interactive rebase
During a long day of coding, your history might look like this:
1a2b3c4 - Initial work5d6e7f8 - Added UI9g0h1i2 - Typo fix3k4l5m6 - Added more UI7n8o9p0 - Fix typo again
This is messy. You don't want your teammates to see your typos. You want to "Squash" those 5 commits into one perfect commit: "Implement Feature X with UI".
This is where Interactive Rebase comes in.
1. What is Interactive Rebase?
Interactive rebase (git rebase -i) opens a text editor and allows you to rewrite the history of your current branch. You can:
- Pick: Keep a commit as-is.
- Reword: Keep the code but change the message.
- Squash: Combine a commit into the one above it.
- Drop: Delete a commit entirely.
- Reorder: Change the sequence of commits.
2. How to Start
You usually rebase back to a specific commit or a number of commits. For example, to clean up your last 5 commits:
git rebase -i HEAD~5
Git will open your text editor with a list of the 5 commits. It will look like this:
pick 1a2b3c4 Initial work
pick 5d6e7f8 Added UI
pick 9g0h1i2 Typo fix
pick 3k4l5m6 Added more UI
pick 7n8o9p0 Fix typo again
The Action
You then change the word pick to your desired action. To squash the typos into the UI commits, you would do:
pick 1a2b3c4 Initial work
pick 5d6e7f8 Added UI
squash 9g0h1i2 Typo fix
pick 3k4l5m6 Added more UI
squash 7n8o9p0 Fix typo again
When you save and close the editor, Git will "re-play" the history according to your instructions.
3. The Power of "Fixup"
fixup is like squash, but it automatically discards the commit message of the squashed commit. It’s perfect for those "OOPS!" commits where you just fixed a semicolon.
4. The Golden Rule Still Applies
NEVER use interactive rebase on commits you have already pushed.
Just like a regular rebase, this tool rewrites SHA-1 IDs. If you squash 3 commits into 1 on a shared branch, you will break everyone else's repository.
graph TD
History["Messy History (A -> B -> Typo -> C -> BugFix)"]
Rebase["git rebase -i"]
Result["Clean History (A -> B -> C)"]
History --> Rebase
Rebase -- "Squash & Fixup" --> Result
Lesson Exercise
Goal: The History Clean-up.
- Create 3 commits in your
git-practicerepo with silly names (e.g.,temp 1,temp 2,final). - Run
git rebase -i HEAD~3. - In the editor, change the last two
pickwords tosquash. - Save and close.
- Git will ask you for a new, combined message. Write one.
- Run
git log --oneline. Notice your 3 commits have become one single, high-quality commit.
Observation: You'll realize that you can work "dirty" while you're focused on coding, and then "clean up" your mess before anyone else sees it.
Summary
In this lesson, we established:
- Interactive rebase is the professional tool for cleaning up history.
pick,squash,fixup, andrewordare the most common operations.- It allows you to present a perfect, logical narrative of your work to your team.
- Remember: Rewrite local history, but never shared history!
Next Lesson: Now that your history is perfect, how do you mark a specific milestone? Welcome to Lesson 5: Tagging and releases.