Rewriting history · lesson 4 of 6
Commands and ideas in this lesson
Agit rebase -ichoose what happens to each commit
Bpick, reword, squash, fixup, dropthe todo list's actions
Cgit commit --fixupmark a commit as a fix for an earlier one
Deditstop to change or split a commit

Interactive rebase

Last updated

Ana's bibingka branch has seven quick commits: "bibingka", "forgot eggs", "wip", "more". Before she opens a pull request, she tidies them. git rebase -i lists the commits and lets her decide, one by one, what happens to each: keep, reword, squash together, reorder or drop. In this lesson you'll tidy seven commits into three.

By the end of this lesson you can

  • Start an interactive rebase and read its todo list
  • Reword, squash, fixup, reorder and drop commits
  • Use git commit --fixup and --autosquash to tidy as you go
  • Split a commit with edit

The todo list

git rebase -i main (on a branch) lists the branch's commits since main, oldest first, each with an action. In a terminal it opens in your editor:

The todo list
pick 572d243 bibingka
pick fa12281 ingredients
pick 762aa94 forgot eggs
pick 9536c25 Say where the photos are
pick 2412563 wip
pick 5fdb82e more
pick bfac2bd steps

Change the word at the start of each line, move lines to reorder, save and close. Git then replays the commits as you said:

ActionWhat it does
pickkeep the commit as it is
rewordkeep it, but change its message
editkeep it, but stop there so you can change it
squashjoin it into the commit above, and combine the messages
fixupjoin it into the commit above, keeping only the one above's message
dropleave it out

The sandbox shows the list as cards, each with an action menu and buttons to move it up or down.

Squashing, watched

$ git log --oneline -8
bfac2bd (HEAD -> bibingka) steps
5fdb82e more
2412563 wip
9536c25 Say where the photos are
762aa94 forgot eggs
fa12281 ingredients
572d243 bibingka
67e435d (main) Cover the leche flan with foil
  1. bfac2bd HEAD →bibingka steps
  2. 5fdb82e more
  3. 2412563 wip
  4. 9536c25 Say where the photos are
  5. 762aa94 forgot eggs
  6. fa12281 ingredients
  7. 572d243 bibingka
  8. 67e435d main Cover the leche flan with foil
  9. ba04ca2 Add lumpiang shanghai recipe
  10. 98a5edc Add a script that builds the index page

and 7 older commits

1 of 3 Seven commits on bibingka. “ingredients” and “forgot eggs” are really part of the first one.

Tidy the bibingka branch

Tidy the seven commits into three, each following lesson 1.7:

  1. the recipe's first version (bibingka, ingredients, forgot eggs);
  2. the README change, on its own;
  3. the rest of the recipe (wip, more, steps).

Use reword or squash to give each a good message. The recipe must end up exactly as it is now.

Git sandbox
  1. Start an interactive rebase onto main
  2. Three commits on top of main
  3. Each message follows lesson 1.7
  4. The README change is still there, in a commit about photos
  5. The recipe is unchanged: salted egg, and baked for 25 minutes

Working folder

no changes

Staging area

nothing staged

Repository

17 commits on bibingka

You are in ~/kusina on bibingka, a branch with seven quick commits nobody has seen yet.

Commit graph

  1. bfac2bd HEAD →bibingka steps
  2. 5fdb82e more
  3. 2412563 wip
  4. 9536c25 Say where the photos are
  5. 762aa94 forgot eggs
  6. fa12281 ingredients
  7. 572d243 bibingka
  8. 67e435d main Cover the leche flan with foil
  9. ba04ca2 Add lumpiang shanghai recipe
  10. 98a5edc Add a script that builds the index page
  11. d078487 Make the sinigang steps clearer
  12. 145dc1e Add pancit bihon recipe
  13. 52d1f4d Say how much salt goes in the adobo
  14. 1c17fad Add a README
  15. 9c53888 Add leche flan recipe
  16. 1f359a2 Add sinigang recipe

and 1 older commit

The README commit was fourth; to make it second, move it up a place in the list. Moving a commit past others it doesn't overlap with is safe. Start over if it goes wrong, or git reset --hard ORIG_HEAD straight after.

Tidying as you go: --fixup

If you notice a mistake in an earlier commit while you're still working, you can mark the fix for it as you commit:

  1. git commit --fixup 572d243 makes a commit called fixup! bibingka.
  2. Later, git rebase -i --autosquash main puts it straight after bibingka with the action fixup already chosen. You just confirm.

git config --global rebase.autosquash true makes every rebase -i do that.

Splitting a commit with edit

A commit that does two things can be split. Mark it edit; the rebase stops just after it. Then:

  1. git reset HEAD~1 undoes the commit but keeps its changes in your working folder (lesson 2.5).
  2. git add the first part and commit; git add the rest and commit again (or use git add -p, lesson 1.5).
  3. git rebase --continue.
Splitting a commit
git rebase -i main      (mark the commit "edit")
Stopped at 9536c25...  Say where the photos are
git reset HEAD~1
git add README.md
git commit -m "Say where the photos are"
git add bibingka.md
git commit -m "Say how many bibingka serves"
git rebase --continue
Successfully rebased and updated refs/heads/bibingka.

Spot the trap: squashing away something you’ll need

Spot the trapAna

Ana’s branch has a recipe commit and, separately, a commit that renames every recipe file. She squashes them into one, “Add bibingka and rename files”, and it’s merged.

What happens if…

Check what you learned

In the todo list, which action joins a commit into the one above and keeps only the above one’s message?
In which order does the todo list show commits?
When should you not tidy commits with rebase -i?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git