Interactive rebase

Intermediate · 8 min

Rewrite a branch before sharing it: squash noise commits, fix a message, drop a mistake, and use fixup commits with autosquash, all via the rebase todo list.

Before thisRebasing a branchAmending the last commitForce pushing safely

Philomatics · 8 min

Interactive rebase

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by PhilomaticsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `git rebase -i <base>` opens a todo list of the commits after `<base>`, oldest first, and replays them according to the command you put on each line.
  • `pick` keeps a commit, `reword` changes its message, `squash` and `fixup` fold it into the commit above (squash keeps both messages), `drop` deletes it and `edit` pauses so you can change it.
  • Reordering lines reorders commits, and a conflict while replaying stops the rebase just as it does in a normal rebase.
  • `git commit --fixup=<commit>` plus `git rebase -i --autosquash` marks and then folds a correction into the right commit automatically.
  • Every rewritten commit gets a new hash, so only rewrite commits you haven't shared, or be ready to force push a private branch.

The problem: your history is a draft

While building the search command for jot, my commits looked like this:

eb6540c fix typo again
8bc944d Add docs for serach
7cb1834 make search case insensitive
1865566 wip
05eee6b Add search
bc105e7 Add jot.sh with add and list

That is honest, and unreadable. A reviewer doesn’t want to see “wip” or a typo fix for a typo in the previous commit. You’d like to present two clean commits: the feature and its docs. Interactive rebase lets you edit a series of commits as if it were a document, before it leaves your machine.

The todo list

git rebase -i main asks Git to replay every commit on this branch that isn’t on main, and first lets you edit the plan. Git 2.54 shows:

pick 05eee6b # Add search
pick 1865566 # wip
pick 7cb1834 # make search case insensitive
pick 8bc944d # Add docs for serach
pick eb6540c # fix typo again

# Rebase bc105e7..eb6540c onto bc105e7 (5 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending

(Hashes will differ on your machine.) The list is oldest first, the reverse of git log, because Git executes it top to bottom. Each line is a command, a commit and a comment with its subject. Edit the commands, save, close the editor, and Git does the replay.

Command What it does
pick Keep the commit as is
reword Keep the changes, stop to edit the message
edit Stop after applying it so you can amend or split it
squash Fold into the previous commit, combine both messages
fixup Fold into the previous commit, discard this message
drop Delete the commit

You can also reorder the lines, which reorders the commits.

A real rewrite

I wanted: keep “Add search”, fold “wip” in silently, fold the case-insensitive change in with its message combined, reword the docs commit, and drop the last typo commit. The edited todo was:

pick 05eee6b # Add search
fixup 1865566 # wip
squash 7cb1834 # make search case insensitive
reword 8bc944d # Add docs for serach
drop eb6540c # fix typo again

Running it (I scripted the editors so the run was repeatable) printed:

Rebasing (2/5)Rebasing (3/5)[detached HEAD b4f40fb] Add case-insensitive search
Rebasing (4/5)[detached HEAD 0aadfe9] Add docs for search
Rebasing (5/5)Successfully rebased and updated refs/heads/search.
$ git log --oneline
0aadfe9 Add docs for search
b4f40fb Add case-insensitive search
bc105e7 Add jot.sh with add and list

Five messy commits became two clear ones. Notice “detached HEAD” in the output. During a rebase, Git builds the new commits on a detached HEAD and only moves the branch name at the end. That is also why every commit has a new hash: it is a new commit with the same changes but a different parent.

Fixup commits and autosquash

Spotting a mistake in an old commit mid-work is common. Instead of stopping to rewrite, make a correction commit and fold it in later:

$ git commit --fixup=HEAD~1
[search b90b3a8] fixup! Add usage section
$ git rebase -i --autosquash main
Successfully rebased and updated refs/heads/search.
$ git log --oneline -3
8b472e6 Add regex flag
4cf7327 Add usage section
0aadfe9 Add docs for search

--fixup named its target in the subject, fixup! Add usage section. With --autosquash, Git moved that commit under its target and set it to fixup, so the todo list needed no manual editing. (I passed GIT_SEQUENCE_EDITOR=true to accept the list as is.) git config rebase.autoSquash true makes it the default.

Getting out

If you change your mind mid-rebase, nothing is lost:

$ git rebase -i main      # I set the first commit to "edit"
Once you are satisfied with your changes, run
  git rebase --continue
$ git rebase --abort

--abort puts the branch back as it was. --continue resumes after you resolved a conflict or finished an edit. And even after a finished rebase, the old commits sit in the reflog.

Rules of thumb

  • Rewrite only commits you haven’t pushed, or that live on a branch only you use. After rewriting pushed commits you will need --force-with-lease.
  • Make a branch pointer before a big rewrite (git branch backup) if you’re nervous.
  • Interactive rebase is the editing tool, and resetting is the blunt one. Use rebase when you want to keep some of the commits.

What the video covers

Philomatics’ “git interactive rebase - Undo, Edit & Squash git commits with a single command” is about eight minutes and from around 2024. From its title and description it covers undoing, editing and squashing commits with rebase -i. I judge English from the title, description and channel only, and I could not read a transcript, so I can’t say whether it covers --autosquash. The Modern SDE’s longer lesson, which includes split and reorder, is linked as a runner-up.

Where this fits

Rebase rewrites a series of commits. Sometimes you only want to move the branch pointer itself: reset, soft, mixed and hard.

Check yourself

  1. 01In the todo list of `git rebase -i main`, in what order are commits listed?

    Oldest first, the opposite of `git log`. Git replays them from the top down.

  2. 02What is the difference between `squash` and `fixup`?

    Both fold a commit into the one above it. `squash` combines the messages and opens an editor, `fixup` discards the folded commit's message.

  3. 03How do you recover if an interactive rebase goes wrong halfway?

    `git rebase --abort` returns the branch to how it was. After a finished rebase, the reflog still has the old tip.

  4. 04What does `git commit --fixup=<hash>` do, and what does it need afterwards?

    It creates a commit titled `fixup! <original subject>`. Run `git rebase -i --autosquash <base>` and Git will place it right after the target and mark it `fixup` for you.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark