git rebase -i HEAD~n opens a "todo" list of the last n commits, oldest first. Rearranging those lines rewrites history in that order.
Workflow:
git rebase -i HEAD~5 — opens editor
- Swap lines to reorder.
- Save & quit. Git replays commits in the new order.
Conflicts can pop up if a later commit depended on the rearranged one — Git pauses, you resolve, then git rebase --continue.
Use cases:
- Move a refactor to before the feature that uses it — easier review.
- Group related changes together so reviewers see one story per commit.
- Push security fixes earlier in the sequence so they land first.
Always rebase before sharing the branch. Reordering commits already pushed forces collaborators to reset.