Course outline · 0% complete

0/28 lessons0%

Course overview →

Interactive rebase, gently

lesson 9-2 · ~9 min · 25/28

Editing your last few commits

Real work is messy. By the time a feature is finished, the branch often reads wip, fix typo, actually works now, which is an honest record of your afternoon and a poor thing to hand a reviewer from lesson 8-2.

Interactive rebase is Git's editor for recent history.

$ git rebase -i HEAD~3

That means let me rework my last three commits. Git opens a todo list in your editor, one line per commit, oldest first.

pick a1b2c3d Add search box
pick e4f5a6b wip
pick c7d8e9f fix typo in search box

The important thing to understand about this screen is that you are not editing code here. You are editing the plan. Change the word at the start of each line, save, close the editor, and Git replays the commits according to your instructions.

That indirection is what makes interactive rebase approachable. Nothing happens while the file is open, so a mistake is one editor quit away from being nothing at all.

The four verbs worth knowing

VerbEffect
pickkeep the commit as is
rewordkeep it, but let me rewrite the message
squashcombine this commit into the one above it
dropdelete the commit entirely

A squash produces one commit carrying both sets of changes, and Git offers you both original messages so you can write the combined one.

Cleaning the earlier example means squashing the two fix-up commits into the real one.

pick a1b2c3d Add search box
squash e4f5a6b wip
squash c7d8e9f fix typo in search box

The result is a single commit, Add search box, containing all the work. The reviewer sees one clean, logical change instead of three commits and a story about your typo.

Since this is a rebase, everything from lesson 9-1 applies. The surviving commits are new objects with new hashes, so tidy up before pushing rather than after.

Two safety nets are worth remembering while you learn this. git rebase --abort backs out of a rebase in progress, exactly like git merge --abort from lesson 6-2, and the reflog from lesson 4-3 still remembers the original commits afterwards.

The todo list you editThe resultpick a1b2c3d Add search boxsquash e4f5a6b wipsquash c7d8e9f fix typo9c8b7a6Add search boxone commit, all threesets of changesEach squash folds its line into the line above, so the two fix-ups disappear into the real commitand Git prompts you once for the combined message. New hash, because every replayed commit is a copy.
An interactive rebase todo list where two squash lines fold their commits into the pick above them, producing a single tidy commit.

Changing pick to squash combines that commit into the one above it, so their changes end up in a single commit.

Git folds the commit into its predecessor and then prompts you to write one message for the combined result, offering both original messages as a starting point.

The verb to keep distinct from it is drop, which deletes a commit outright along with its changes. Squash preserves the work and merges it into a neighbor, while drop discards the work entirely, and confusing the two is an easy way to lose a fix you meant to keep.

Squashing and rewording your branch's commits is safe while they exist only in your local repository and have not been pushed.

Interactive rebase rewrites history, so the golden rule from lesson 9-1 governs it exactly as it governs a plain rebase. Local-only commits are fair game, shared commits are off-limits.

The habit that falls out of this is worth adopting deliberately: tidy first, push second. Cleaning up a branch takes a minute before the first push and becomes a team-wide problem after it.

Put squash on the last two lines.

Leaving the first commit as pick and marking the following two as squash melts all three into a single commit, and Git prompts you to write its final message from the three originals.

The todo list accepts one-letter shorthands as well, so s does the same job as squash, and p, r, and d stand in for pick, reword, and drop. The full words are clearer while you are learning, and both forms appear in other people's screenshots.