Class 19 of 50 • Branching & Merging

Git Interactive Rebase: Squashing, Rewording, Dropping & Reordering Commits

When developing a complex feature, your local commit history often looks messy: "wip", "fix typo", "tested again", "really fixed now". Submitting this to code review is unprofessional. Interactive Rebase (git rebase -i) lets you edit your commit history before anyone else sees it.

Launching Interactive Rebase

To launch interactive rebase, pass the -i flag along with how far back into history you want to go:

# Rebase the last 4 commits on your current branch:
$ git rebase -i HEAD~4

# Or rebase all commits on your branch since it diverged from main:
$ git rebase -i main

Git opens your text editor displaying a list of your commits, oldest at the top to newest at the bottom, accompanied by a command cheat sheet:

pick a1b2c3d feat(auth): add login form markup
pick 4e5f6g7 fix typo in input placeholder
pick 8h9i0j1 add password strength validator
pick 2k3l4m5 oops remove console.log

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = meld into previous commit (discard commit, keep message)
# f, fixup = like "squash", but discard this commit's log message
# d, drop = remove commit entirely

The 4 Most Powerful Interactive Rebase Operations

1. Squashing Commits (squash and fixup)

To combine multiple small commits into a single comprehensive commit, change pick to squash (or s) or fixup (or f):

pick a1b2c3d feat(auth): add login form markup
f 4e5f6g7 fix typo in input placeholder
s 8h9i0j1 add password strength validator
f 2k3l4m5 oops remove console.log

Here, the typo fix and console.log cleanup will be silently absorbed into the first commit (fixup), while the validator's commit message will be combined with the first commit's message (squash).

2. Rewording a Commit Message (reword)

Change pick to reword (or r) next to any commit. Git will pause when it reaches that commit and prompt you to rewrite the title and body.

3. Dropping a Commit Entirely (drop)

Change pick to drop (or d), or simply delete the entire line from the file. Git will completely excise that commit from history!

4. Reordering Commits

Simply cut and paste the lines in the editor to change the chronological sequence in which commits are applied.

Save and Execute

Once you make your adjustments, save and close the editor. Git executes each command step-by-step. If you used squash, Git will open a final editor screen asking you to finalize the combined commit message.

Explore Related Git & GitHub Tutorials & Next Steps

Strengthen your version control mastery by studying these connected classes:

Rebase Fundamentals & Golden Rule

Review the mechanics of replaying commits on top of another base branch.

Amending Commits with --amend

Modify single commits quickly without invoking full interactive editor sessions.

GitHub Pull Requests & PR Polish

Squash messy WIP commits into crisp atomic units before code review.

Reflog Safety Net for Rebasing

Revert botched interactive rebase commands with zero data loss.

Frequently Asked Questions (FAQ) - Student & Interview Review

`squash` combines the commit's code changes into the previous commit AND prompts you to combine both commit messages into one. `fixup` combines the code changes into the previous commit but automatically discards the fixup commit's message, keeping your history concise.
In `git log`, the newest commit is at the top. But in `git rebase -i`, commits are listed chronologically (oldest at the top, newest at the bottom) because Git replays them from the oldest to the newest.
Yes! At any point while resolving or editing, run `git rebase --abort`. Git will cancel the entire session and return your repository to the exact state it was in before you launched the rebase.

Next Step in Your Git & GitHub Learning Path:

Advance your software engineering skills with the next topic in our synchronized curriculum.

Continue to git stash Operations → Previous: ← git rebase Fundamentals | Full Syllabus

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!