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:
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:
| Action | What it does |
|---|---|
pick | keep the commit as it is |
reword | keep it, but change its message |
edit | keep it, but stop there so you can change it |
squash | join it into the commit above, and combine the messages |
fixup | join it into the commit above, keeping only the one above's message |
drop | leave 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
bfac2bdHEAD →bibingka steps5fdb82emore2412563wip9536c25Say where the photos are762aa94forgot eggsfa12281ingredients572d243bibingka67e435dmain Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index page
and 7 older commits
Tidy the bibingka branch
Tidy the seven commits into three, each following lesson 1.7:
- the recipe's first version (bibingka, ingredients, forgot eggs);
- the README change, on its own;
- 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.
- Start an interactive rebase onto main
- Three commits on top of main
- Each message follows lesson 1.7
- The README change is still there, in a commit about photos
- 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
bfac2bdHEAD →bibingka steps5fdb82emore2412563wip9536c25Say where the photos are762aa94forgot eggsfa12281ingredients572d243bibingka67e435dmain Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add 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:
git commit --fixup 572d243makes a commit calledfixup! bibingka.- Later,
git rebase -i --autosquash mainputs it straight afterbibingkawith the actionfixupalready 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:
git reset HEAD~1undoes the commit but keeps its changes in your working folder (lesson 2.5).git addthe first part and commit;git addthe rest and commit again (or usegit add -p, lesson 1.5).git rebase --continue.
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
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
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.
- Git documentation: git-rebase, Interactive mode: the todo list, its commands, and splitting commits
- Git documentation: git-commit, --fixup: fixup! commits for --autosquash
- Pro Git: Rewriting History: reordering, squashing and splitting