Sub topic 7
Reordering and dropping commits
This chapter assumes you can read the todo list from git rebase -i, know pick, and have a feature branch with several local commits that you have not pushed. Reordering and dropping both work by editing the todo list. You change the lines, save, and Git replays the commits in the new arrangement.
How the todo list controls the result
Git applies the todo list from top to bottom, and the top line is the oldest commit. If you move a line, that commit is replayed at a different point. If you delete a line or mark it drop, Git does not replay that commit, and its changes are absent from the new branch.
Suppose git log --oneline -n 4 shows this:
a6b2c33 Add login tests
7d90e1f Debug logging
51ab9c0 Add validation
3f2a1b9 Add login formRunning git rebase -i HEAD~4 opens the list in reverse order, oldest first:
pick 3f2a1b9 Add login form
pick 51ab9c0 Add validation
pick 7d90e1f Debug logging
pick a6b2c33 Add login testsReordering commits
To reorder, cut a line and paste it somewhere else. Say you want the tests to come right after the form, before the validation. Move the last line up:
pick 3f2a1b9 Add login form
pick a6b2c33 Add login tests
pick 51ab9c0 Add validation
pick 7d90e1f Debug loggingSave and close the editor. Git replays the four commits in the new order and creates new commits for each one that changed position or parent.
Reordering is only safe when the commits touch different parts of the code. If two commits edit the same lines, the swapped order can produce a conflict, because the later commit no longer applies on top of the state it was written against. Chapter 8 covers resolving those conflicts.
Dropping commits
There are two equivalent ways to remove a commit:
- Change
picktodrop(ord) on its line. - Delete the whole line.
Using drop is clearer because the intent stays visible in the file while you edit. Deleting a line works the same, but a deleted line is easy to remove by accident. Git does not warn you about it by default.
This list drops the debug commit:
pick 3f2a1b9 Add login form
pick a6b2c33 Add login tests
pick 51ab9c0 Add validation
drop 7d90e1f Debug loggingAfter the rebase, the branch has three commits and the debug logging is gone from the history and from the files, unless a later commit relied on it.
Checking the result
After the rebase finishes, compare the new series with the original. The old tip is saved in ORIG_HEAD:
git log --oneline origin/main..HEAD
git diff ORIG_HEAD HEADIf you only reordered, git diff ORIG_HEAD HEAD prints nothing, because the final tree is identical. If you dropped a commit, the diff shows exactly what that commit contributed.
The sequence in one picture
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Conflict right after reordering | Two commits change the same lines | Resolve it, or restore the original order with `git rebase --abort` |
| A commit vanished | A line was deleted or marked `drop` by mistake | Run `git reset --hard ORIG_HEAD` and redo the rebase |
| Tests fail after dropping a commit | A later commit depended on it | Keep the commit, or fix the later commit |
| Nothing happened | The list was left empty, so the rebase aborted | Start again |
Exercise
Your todo list has four lines, and the last one is pick 7d90e1f Debug logging. You want to remove that commit but keep its line visible while you finish editing. What do you change, and which commit is replayed first?
Show answer
Change pick to drop on that line, so it reads drop 7d90e1f Debug logging. The first line in the file is replayed first, and that is the oldest commit.