Sub topic 4
Reading and editing the todo list
This chapter assumes you can stage, commit and read git log, and that you know how to save and close your text editor. You have already started git rebase -i. Now you need to read the file it opens.
What the todo list is
When you run git rebase -i HEAD~4, Git opens a text file called the todo list. It holds one line per commit that will be replayed. Git reads the file from top to bottom when you save and close the editor. Each line is a command, a commit hash and the commit subject.
pick 3f2a1b9 Add login form
fixup 8c4d7e2 Fix typo in login form
reword 51ab9c0 Add validation
drop 7d90e1f Debug logging
pick a6b2c33 Add login testsThe order is the reverse of git log. The oldest commit is at the top, because that is the order Git replays them. Below the commits, Git adds comment lines that start with #. These list every command and are ignored when the file is read.
The commands
Each command also has a one-letter short form, such as p for pick.
| Command | Short | Effect |
|---|---|---|
| `pick` | `p` | Keep the commit as it is. |
| `reword` | `r` | Keep the changes and stop to let you edit the message. |
| `edit` | `e` | Stop after applying the commit so you can change it. |
| `squash` | `s` | Merge into the previous commit and combine both messages. |
| `fixup` | `f` | Merge into the previous commit and discard this message. |
| `drop` | `d` | Remove the commit. |
Later chapters cover reword, edit, squash and fixup in detail. Here you only need to know that each line is an instruction about one commit.
Editing the list
You change the rebase by editing the file. Three kinds of edit cover most cases:
- Change the first word of a line to change what happens to that commit.
- Move a line up or down to reorder commits.
- Delete a line, or write
drop, to remove a commit.
A line that starts with squash or fixup combines with the line above it. In the example, fixup 8c4d7e2 folds into pick 3f2a1b9. Keep the two lines together, in that order.
Running a rebase from the todo list
Look at the log, then open the list:
git log --oneline -n 5
git rebase -i HEAD~4Edit the file as shown above, then save and close it. Git replays the commits. If every command succeeds, the branch ends up with four commits replaced by three: the combined login form, the reworded validation commit and the tests. Git pauses at reword so you can edit the message.
Common errors
- Starting with
squashorfixup. The first line has no previous commit to merge into. Git reports an error such ascannot 'squash' without a previous commit. Change it topick, or move a commit above it. - Expecting newest-first order. If you read the list like
git log, you will fold commits in the wrong direction. The line above is always the older commit. - Editing the hash. Leave the hash and subject alone. The subject is only a label for you. Git identifies the commit by its hash.
- Closing the editor too early. Saving an unfinished edit starts the rebase with that list. If you saved by mistake, run
git rebase --abort. - Merge commits missing. By default, the list leaves out merge commits and flattens the history. You need
--rebase-mergesto keep them, which is outside this chapter.
Exercise
A todo list has these lines in order: pick a1, squash b2, pick c3, fixup d4. Which commits does each combined commit contain, and what happens to the messages?
Show answer
Commit b2 merges into a1, and Git lets you combine and edit both messages. Commit d4 merges into c3, and d4's message is discarded. The result is two commits: a1+b2 and c3+d4.