Real work leaves a messy trail: wip, fix typo, debug commits. Before a review, teams tidy it into a few commits that each make sense. git rebase -i (interactive) lets you rewrite the last few commits by editing a todo list:
git rebase -i HEAD~5
hint: Waiting for your editor to close the file...The editor panel opens with one line per commit, oldest first, plus git's notes:
pick cb790f9 # Add a
pick bf1b025 # WIP b
pick e2b972f # fix b
pick 600dd40 # Add c
pick 90e0dea # debug junkChange the word at the start of a line to decide what happens to that commit:
| Command | What it does |
|---|---|
pick | keep the commit as it is |
reword | keep it, but edit its message (git opens the editor for it) |
squash | combine it with the commit above, and edit the combined message |
fixup | combine it with the commit above, and keep the above commit's message |
drop | remove the commit (deleting the line does the same) |
edit | stop at this commit so you can change it, then git rebase --continue |
Moving a line moves the commit. Save, and git replays the list from the top:
pick cb790f9 # Add a
reword bf1b025 # WIP b
fixup e2b972f # fix b
pick 600dd40 # Add c
drop 90e0dea # debug junkSuccessfully rebased and updated refs/heads/main.
git log --oneline
46b7b78 (HEAD -> main) Add c
1b1a401 Add b
cb790f9 Add a
27abc14 Start the shopFive commits became three. Everything rewritten got a new hash.
Objectives
- Fold the typo fix into its commit.
- Drop the debug commit.
- Give the last commit a real message.