Skip to content

git rebase -i: clean up commit history before merging

Use interactive rebase (git rebase -i) to squash, fixup, reword, reorder, edit, and drop commits so your branch history reads clearly before you merge.

When your feature branch is a pile of “wip”, “fix typo”, and “actually fix it” commits, git rebase -i lets you rewrite that history into a clean sequence before it lands on the main branch. You open an editor with a todo list of your commits, change the action next to each one — squash, fixup, reword, reorder, edit, or drop — save, and Git replays the commits with your instructions applied. The result is history that reads like it was written on purpose. This guide covers the todo-list actions, a full squash-and-reword pass, resolving conflicts mid-rebase, and how to recover if you make a mess.

What you’ll accomplish

  • Open the interactive todo list for the last N commits with git rebase -i.
  • Combine noisy commits into meaningful ones with squash and fixup.
  • Fix a bad commit message with reword, and change the order of commits by moving lines.
  • Stop mid-rebase with edit to amend a commit, then git rebase --continue.
  • End with a branch whose git log --oneline tells a clear story, verified before you push.

Prerequisites

  • Git 2.x on your machine (git --version). Interactive rebase has shipped for years; nothing extra to install.
  • A feature branch with a few commits you want to tidy. This guide assumes you branched off main and have not merged yet.
  • A backup branch before you start. Rebase rewrites commits, which means new SHAs and the chance to lose work. Make a cheap safety net first: git branch backup/my-feature. If anything goes wrong you can reset to it.
  • Never rebase history that others have already pulled. Rewriting shared commits forces everyone downstream into a painful re-sync. Interactive rebase is for your own local, un-pushed (or solely-yours) branches. More on this in the last section.
  • Know your default editor. Git opens the todo list in $GIT_EDITOR / core.editor (often Vim or nano). Set it once if you have a preference: git config --global core.editor "nano".

How it works

A rebase takes a range of commits and replays them, one at a time, onto a new base. Interactive rebase adds a planning step in the middle: before replaying, Git hands you a todo list — one line per commit, oldest at the top, each line starting with an action verb. You edit that list to say what should happen to each commit, save and close the editor, and Git executes the plan top to bottom.

Interactive rebase todo list transforming six messy commits into three clean commits through pick, squash, and reword actions

The actions you can put in front of each commit are the whole vocabulary of the feature:

  • pick — keep the commit as-is (the default for every line).
  • reword — keep the changes, but stop to edit the commit message.
  • edit — stop at this commit so you can amend its content, split it, or run commands, then continue.
  • squash — merge this commit into the one above it, and combine both messages into one editor pass.
  • fixup — like squash, but discard this commit’s message and keep only the message above. This is the workhorse for “fix a typo I made three commits ago”.
  • drop — delete the commit entirely (or just delete its line from the list).
  • Reorder — move a line up or down to change the order commits are applied. Git replays them in the new order.

Because squash and fixup fold a commit into the line above, order matters: the commit you are absorbing must sit directly below the one it joins.

Before and after a squash: five commits including two fixup lines collapse into two clean commits on the branch

Step-by-step

1. Confirm what you are about to rewrite. Look at the commits on your branch that are not on main. This is exactly the set you will be reordering and squashing, so check it before you touch anything.

git log --oneline main..HEAD
9f3a1c7 (HEAD -> feature/checkout) fix typo in comment
2b8e4d0 actually handle the empty-cart case
6c1f9a2 wip empty cart
a4d7e83 add checkout total helper
1e0b5f6 wip checkout

2. Start the interactive rebase. Point Git at the commit before the range you want to edit. HEAD~5 means “the last five commits”, which matches the five lines above. You can also rebase everything since you branched with git rebase -i main.

git rebase -i HEAD~5

Git opens your editor with the todo list. Note it shows commits oldest first — the reverse of git log.

pick 1e0b5f6 wip checkout
pick a4d7e83 add checkout total helper
pick 6c1f9a2 wip empty cart
pick 2b8e4d0 actually handle the empty-cart case
pick 9f3a1c7 fix typo in comment

# Rebase 3d9a0f1..9f3a1c7 onto 3d9a0f1 (5 commands)
# Commands:
#  p, pick   = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit   = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup  = like "squash", but discard this commit's log message
#  d, drop   = remove commit

3. Edit the plan. Change the action verbs to describe the history you want. Here we fold each “wip” into its real commit, reword the first, and drop the trailing typo fix into the commit it belongs to. Leave the commits in place; only the verbs change.

reword 1e0b5f6 wip checkout
squash a4d7e83 add checkout total helper
pick 6c1f9a2 wip empty cart
fixup 2b8e4d0 actually handle the empty-cart case
fixup 9f3a1c7 fix typo in comment

Save and close the editor. Git starts replaying from the top.

4. Handle the reword prompt. Because line one is reword, Git stops and reopens the editor with that commit’s message so you can rewrite it. The squash on line two will also open an editor to combine messages. Type a real message.

Add checkout total helper

Introduces the running-total calculation used by the cart summary.
[detached HEAD 7a2c4e9] Add checkout total helper
 2 files changed, 34 insertions(+)

5. Let the fixups run. The two fixup lines need no input — Git folds their changes into “wip empty cart” and keeps that message. When the plan finishes with no conflicts, Git prints a success line and moves HEAD back onto your branch.

Successfully rebased and updated refs/heads/feature/checkout.

6. Reorder commits when you need to. To change the order, simply move the lines. Say the total helper should land before the checkout scaffolding — cut its line and paste it above. Git will replay in the new top-to-bottom order (watch for conflicts if the reordered commits touch the same lines).

pick a4d7e83 add checkout total helper
pick 1e0b5f6 wip checkout

7. Use edit to amend a commit’s contents. Set a line to edit when you need to change what a commit did, not just its message — split it, remove a stray file, or fix code. Git stops with HEAD at that commit so you can work, then you stage and continue.

# after Git stops at the 'edit' commit:
git reset HEAD~1          # unstage the commit's changes to split them, optional
# make edits, then:
git add src/checkout.js
git commit --amend --no-edit
git rebase --continue
Successfully rebased and updated refs/heads/feature/checkout.

Validate it works

Check that the history now reads the way you intended. The oneline log should show your cleaned-up commits with real messages and no “wip” noise.

git log --oneline main..HEAD
b1d4f88 (HEAD -> feature/checkout) Handle the empty-cart case
7a2c4e9 Add checkout total helper

Confirm the working tree is clean and you are not stuck mid-rebase. A clean status with no “rebase in progress” line means the operation finished.

git status
On branch feature/checkout
nothing to commit, working tree clean

As a final safety check, diff your rewritten branch against the backup you made. If the content is identical (only history shape changed), the diff is empty — which is exactly what you want after a pure squash/reword pass.

git diff backup/my-feature HEAD
(no output — the trees are identical)

Troubleshooting

A conflict stops the rebase mid-flight. When a replayed commit does not apply cleanly, Git pauses and asks you to resolve. Open the conflicted files, fix the <<<<<<< markers, stage them, and continue. Do not run git commit here — git rebase --continue creates the commit for you.

git status                 # shows "Unmerged paths"
# edit the files, resolve markers, then:
git add src/checkout.js
git rebase --continue

You want to bail out entirely. If the rebase is going sideways, abort it. git rebase --abort returns your branch to exactly where it was before you started — no changes, no lost commits.

git rebase --abort

You are on a “detached HEAD” during the rebase. This is normal. While replaying, Git checks out commits without a branch attached, so git status says “HEAD detached”. You do not need to fix it — finishing with git rebase --continue (or --abort) reattaches HEAD to your branch automatically.

You already pushed and now Git rejects your push. After a rebase your local commits have new SHAs, so a normal git push is rejected as non-fast-forward. If the branch is truly yours alone, use the safer force flag, which refuses to overwrite work you have not seen: git push --force-with-lease. If others share the branch, stop — see the next section.

git push --force-with-lease

You rewrote the wrong commits and want them back. Every commit you had is still in the reflog for a while, even “lost” ones. List recent HEAD positions, find the SHA from before the rebase, and reset to it. This is why the backup branch and the reflog together make rebase safe to experiment with.

git reflog
# find the line like: 9f3a1c7 HEAD@{5}: commit: fix typo in comment
git reset --hard HEAD@{5}

For a deeper recovery walkthrough, see git bisect: find the commit that introduced a bug for a related history-navigation technique, and the reflog is the tool of record for undo.

When this is the wrong approach

Interactive rebase rewrites history, and that is exactly why it is dangerous in the wrong place:

  • The branch is shared and already pushed. If teammates have pulled the commits you are about to rewrite, rebasing forces every one of them into a manual re-sync and invites lost work and duplicated commits. On shared branches, add new commits or use git revert; do not rewrite what others already have.
  • You want to preserve the true timeline. Sometimes the messy sequence is the record — a bisect-friendly history, an audit trail, or a teaching example. Squashing it away destroys information. A merge commit that keeps every original commit may serve you better than a flattened branch.
  • The history is a tangle of merges. Rebasing across many merge commits is error-prone and can replay commits that never stood on their own. When a branch has absorbed several merges from main, a plain merge is usually cleaner and safer than trying to linearize it.
  • You are still actively changing the code. Rebase to clean up finished work, right before review or merge — not in the middle of development, where you will just have to redo it.

The authoritative reference is the official git rebase documentation. For the interactive todo-list details specifically, the same page has a dedicated “Interactive Mode” section.

If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.