Course contentsShow
← Git & GitHub
Lesson 80 of 965. Advanced Productivity and WorkflowFree lesson

Understanding the Mechanics of Interactive Rebase

Learn how interactive rebase allows you to modify, reorder, or delete existing commits within your local branch history.

In this lesson, you will learn how to refine your local development history by leveraging the power of interactive rebase to polish, reorder, or remove specific commits before sharing your code.

While a standard rebase moves your entire branch onto a new base, an interactive rebase (git rebase -i) acts like a surgical tool for your commit history. It opens an editor that presents your recent commits, allowing you to rewrite the timeline of your feature branch before you push it to a remote repository.

How it Works

When you trigger git rebase -i <base>, Git presents a list of your commits in reverse chronological order. Next to each commit, you can change the default command pick to one of several powerful alternatives:

  • reword: Change the commit message without altering the code.
  • edit: Pause the rebase at this commit to amend the file contents.
  • squash: Combine this commit with the one above it, merging their messages.
  • drop: Completely remove the commit from your history.
  • Reordering: You can manually cut and paste lines to change the order in which commits are applied to the branch.

Example Workflow

If your history looks like this:

pick a1b2c3d Fix typo
pick e4f5g6h Add feature logic
pick i7j8k9l Fix typo (again)

You can change it to:

pick a1b2c3d Fix typo
squash i7j8k9l Fix typo (again)
pick e4f5g6h Add feature logic

Once you save and close the editor, Git replays your commits one by one, executing your instructions. If you choose squash or edit, Git will pause and prompt you to finish the task. This is an incredible way to turn a "messy" stream of "WIP" (Work In Progress) commits into a clean, professional, and logical sequence that is much easier for your team to review.

Remember: because interactive rebase effectively rewrites history, you should only perform this on local, unshared branches. Once you have cleaned up your work to your satisfaction, your history will look pristine and ready for the next stage of your collaborative workflow.

Key Takeaway: Interactive rebase empowers you to curate your commit history, transforming a series of iterative trial-and-error steps into a polished, professional narrative before your code reaches the remote repository.