Skip to main content
← Back to course

Interactive Rebase: Rewriting Your Own History

Interactive rebase lets you clean up a branch's commit history before sharing it — combining messy "WIP" commits into something coherent, without changing the actual final result.

git rebase -i HEAD~N opens an editor listing your last N commits, each with an action you can change: pick (keep as-is), squash (combine into the previous commit), reword (change the message), drop (remove entirely). Save the file, and Git replays your history according to your edits.

This rewrites commit hashes — only do it on branches you haven't shared yet. Once you've pushed a branch and someone else might have based work on it, rewriting its history with rebase causes real problems for them (their local history no longer matches). The safe rule: rebase freely on your own unpushed or unshared work; avoid it on anything already shared, unless the whole team knows and coordinates around it.

A common real use: squashing "WIP, WIP, actually fix it, fix typo" into one clean commit before opening a Pull Request — even though GitHub's Squash and Merge (from the Pull Requests course) can achieve a similar end result, interactive rebase gives you more control (e.g., combining only some commits, not the entire branch, or reordering them).

Reordering commits is possible, not just editing them. Moving lines in the rebase file's list reorders the commits themselves — useful when you realize commit 3 logically belongs before commit 2.

If a rebase gets confusing partway through, git rebase --abort returns you to exactly where you started — the same safety net as git merge --abort from the Branching & Merging course.

Why this matters for you

The difference between a branch history that reads like a clear story and one that reads like a stream-of-consciousness diary is almost always a quick interactive rebase before sharing — a small habit with a real, visible payoff for anyone reading your history later.

▶️ Before the next lesson

Make three small, deliberately messy commits on a practice branch (e.g., "wip," "fix," "actually works") — you'll clean these up with interactive rebase in this course.