Free — no signup required

Interactive Rebase: Cleaning Up History

2 min read

Don't Publish Messy Work

During active development, commit messages like "wip", "fix typo", and "forgot semicolon" are normal. But these messages pollute the permanent project history that your teammates and future-you will read.

Interactive rebase (git rebase -i) lets you rewrite your local commit history before sharing it. Think of it as editing a draft before publishing — you're reorganizing, combining, and clarifying your work into a coherent narrative.

How It Works

You specify a starting point — how far back you want to edit, either a fixed number of commits or everything since branching from a given point. Git opens your text editor with a list of commits, oldest first, each prefixed with an action keyword:

pick a1b2c3d WIP feature
pick b2c3d4e oops fixed typo
pick c3d4e5f forgot semicolon
pick d4e5f6a Finished feature

You change the action keywords to tell Git what to do with each commit:

Action Effect
pick Keep the commit unchanged (default)
reword Keep the change, edit the commit message
squash Combine with the previous commit, merge both messages
fixup Combine with the previous commit, discard this message
drop Delete the commit entirely
edit Pause rebase here so you can amend the commit

After you save and close the editor, Git replays the commits according to your instructions. If you squash commits, it opens a second editor to write the combined message.

What Actually Happens Under the Hood

Rebase doesn't modify existing commits — it creates new ones. Each replayed commit gets a new SHA-1 hash because its parent pointer has changed. The old commits still exist in the object database (and in the reflog) until garbage collection runs.

This is why the golden rule of rebasing is absolute: never rebase commits that have already been pushed to a shared branch. If your teammates have based work on commit a1b2c3d and you rebase it into x9y8z7w, their history has diverged from yours. The result is a painful forced-push situation that corrupts shared history.

Key Point: Interactive rebase rewrites commit history by creating new commit objects with new hashes. It's a powerful cleanup tool for local branches, but must never be used on commits already shared with others. The reflog is your safety net if a rebase goes wrong.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free