2HAASStart a project
All posts

Guides

Git interactive rebase: clean commits before review

A branch full of wip and typo commits is hard to review. Interactive rebase lets you squash, reorder and reword commits into a history that explains the change.

The 2HAAS team2 min read

While you work, commits are save points: "wip", "fix typo", "try again". That is fine. Before a pull request, those save points are noise. A reviewer wants to read three clear steps, not eleven fragments.

Interactive rebase turns the first kind of history into the second.

Start an interactive rebase

To edit the last four commits:

git rebase -i HEAD~4

Git opens an editor with one line per commit, oldest first.

pick a1b2c3d Add discount field to cart
pick d4e5f6a wip
pick 0718293 Fix typo
pick 4a5b6c7 Add discount tests

Change the word at the start of a line to tell Git what to do with that commit.

Command Effect
pick Keep the commit as it is
reword Keep the changes, edit the message
squash Merge into the commit above and combine the messages
fixup Merge into the commit above and discard this message
edit Stop here so you can change the commit
drop Remove the commit

You can also reorder lines to reorder commits.

pick a1b2c3d Add discount field to cart
fixup d4e5f6a wip
fixup 0718293 Fix typo
pick 4a5b6c7 Add discount tests

Save and close. Four commits become two that each mean something.

Mark fixups as you go

When you fix something that belongs to an earlier commit, say so at commit time:

git commit --fixup a1b2c3d
git rebase -i --autosquash main

Git places each fixup under its target automatically. You only confirm.

Update the remote branch safely

A rebase rewrites commits, so a normal push is rejected. Use:

git push --force-with-lease

Unlike --force, this refuses to overwrite the remote if someone else pushed in the meantime.

Rules that keep you out of trouble

  • Rewrite only your own branch. Never rebase main or a branch other people build on.
  • Rebase before review, not during. Once reviewers have commented, add new commits so they can see what changed.
  • You can always back out. git rebase --abort stops a rebase in progress, and git reflog lists every earlier state of the branch if you need to return to one.

Stay current with main

The same command keeps a feature branch up to date without merge commits:

git fetch origin
git rebase origin/main

Why it is worth the minute

A clean history makes reviews faster, makes git bisect and git revert useful, and tells the next developer why each change was made. That includes you, six months from now.

Keep reading

  1. Guides5 min read

    Build Offline Web Apps with Service Workers

    Discover how to use Service Workers to make your web applications work offline. Learn caching strategies and PWA principles.

  2. Tech News5 min read

    TypeScript 5.5 Beta Enhances Developer Experience

    Learn about the new features in TypeScript 5.5 Beta. Discover how improved control flow analysis and type inference enhance your coding.

  3. Guides5 min read

    Guide to Implementing Web Push Notifications

    Learn how to set up and send web push notifications to re-engage users. This guide covers client-side setup and server-side logic.

Tell us what’s in the way.

A few lines is enough. You get a candid yes, a candid no, or a sharper question.

  • Working software in week four
  • A reply in one business day
  • Handover is the deliverable

Prefer email? hello@2haas.com

The problem, who it is for, and what is in the way right now.

One business day to a real reply. No sales sequence, no call required.