Sub topic 13
Editing, squashing, and dropping local commits with rebase -i
This chapter assumes you know how git reset, git commit --amend, and git reflog work, and that you understand why rewriting shared history is risky. Interactive rebase extends --amend to commits further back than the latest one. It rewrites several local commits in one pass.
What interactive rebase does
git rebase -i (interactive rebase) takes a range of commits, shows them in a todo list, and replays them one by one according to the command you put beside each. Git does not change the old commits. It builds new commits with new hashes and then moves the branch to the last new one. This is the same "rewrite, don't edit" behavior you saw with --amend.
Start it by naming the commit before the first one you want to change:
git rebase -i HEAD~3HEAD~3 means "the three most recent commits". Git opens your editor with the oldest commit first:
pick 1a2b3c4 Add login form
pick 5d6e7f8 Fix typo in login form
pick 9a0b1c2 Add debug loggingThe order is the reverse of git log. Commits are replayed from top to bottom.
The common commands
Change the word at the start of a line, save, and close the editor. Git then applies your list.
| Command | Effect |
|---|---|
| `pick` | Keep the commit as it is. |
| `reword` | Keep the changes and stop to let you edit the message. |
| `edit` | Stop after applying the commit so you can change its content, then continue. |
| `squash` | Merge the commit into the one above it and combine both messages. |
| `fixup` | Merge into the commit above and discard this commit's message. |
| `drop` | Remove the commit and its changes. |
You can also reorder the lines to reorder the commits. Deleting a line drops that commit, so use the explicit drop word to keep your intent clear.
A worked example
Suppose the todo list above has a typo-fix commit and a debug commit that you do not want to ship. Edit the list like this:
pick 1a2b3c4 Add login form
fixup 5d6e7f8 Fix typo in login form
drop 9a0b1c2 Add debug loggingAfter you save, the history changes as shown below.
The result is one clean commit, A'. Its hash differs from A because its content and parent changed. Run git log --oneline to confirm.
Stopping on conflicts and edits
If a replayed commit conflicts with the one before it, for example after you reorder or drop a commit that later commits depend on, Git pauses. Resolve the files, stage them, and continue:
git add login.py
git rebase --continueWith an edit line, Git pauses after applying that commit. You can change files, run git add, run git commit --amend, and then git rebase --continue.
To give up and return to the state before you started, run:
git rebase --abortAutosquash for quick fixes
If you already know a new commit fixes an older one, mark it when you create it:
git commit --fixup 1a2b3c4
git rebase -i --autosquash 1a2b3c4~1Git places the fixup commit under its target and pre-fills the fixup word for you.
Common errors
- Rebasing shared commits. If the commits are already pushed and others use them, the rewritten hashes will conflict with their copies. Use
git revertinstead. - Wrong range.
HEAD~3includes three commits. If you need to change a commit, name its parent, not the commit itself. - Pushing after a rewrite. A normal push is rejected because history diverged. On a personal branch, use
git push --force-with-lease origin my-feature. It refuses to overwrite commits you have not fetched. - Dropping the wrong commit. The old commits stay reachable through
git reflog, so you can recover withgit reset --hardto the earlier position.
Exercise
Your branch has commits A, B, and C (oldest to newest). B is a small typo fix for A, and C is a debug commit you never want to push. Which rebase command do you run, and what does the todo list look like?
Show answer
Run git rebase -i HEAD~3. Edit the list to pick A, fixup B, and drop C. Git folds B into A, keeping A's message, and removes C.
Exercise
You finish a rebase and realize you dropped the wrong commit. The branch is local only. How do you get it back?
Show answer
Run git reflog to find the entry from before the rebase, then run git reset --hard HEAD@{N} with that entry's number. You can also run git rebase --abort if the rebase is still in progress.