Course contentsShow
← Software Engineering
Lesson 68 of 1,9332. Version Control with GitFree lesson

Interactive Rebase for History Cleanup

Use git rebase -i to reorder, squash, edit, or drop commits to create a clean, logical commit history.

Interactive Rebase for History Cleanup

You'll learn how to use Git's interactive rebase tool to sculpt your commit history into a clean, logical story before sharing it with your team.

What Is Interactive Rebase?

Remember from our earlier lesson that git rebase replays commits onto a new base. Interactive rebase (git rebase -i) takes this further—it opens an editor where you can rewrite history by reordering, combining, editing, or removing commits entirely.

Think of it like editing a rough draft of an essay before publishing. Your first attempt might have typos, repeated ideas, or sections in the wrong order. Interactive rebase lets you polish your commit history so reviewers see a clear, logical progression of changes.

How It Works

Run git rebase -i HEAD~3 to interactively rebase the last 3 commits. Git opens an editor showing:

pick a1b2c3d Add user login feature
pick e4f5g6h Fix typo in login form
pick i7j8k9l Update login documentation

You can change pick to commands like:

  • reorder: Move lines to change commit order
  • squash (or s): Merge a commit into the previous one
  • edit: Pause to modify a commit's content
  • drop: Remove a commit entirely

After saving, Git replays your history with the changes applied.

Why Clean History Matters

A messy history with "fix typo" and "oops forgot file" commits clutters git log and makes code archaeology harder. By squashing related commits and organizing changes logically, you help future developers (including yourself) understand why changes were made.

Key Takeaway: Interactive rebase with git rebase -i lets you reorder, squash, edit, or drop commits to transform a messy work-in-progress history into a clean, professional commit sequence before merging.