Gitflow
🔀Reorder Commits Before PRMedium+500 XP
Scenario reference

Reorder Commits Before PR: the situation, step by step

The situation

You committed the refactor AFTER the feature. Reviewers want the refactor first so the feature diff is small.

Bookmark the tip, rewind the branch, then replay the two commits in the order you want.

Medium · 6 steps · +500 XP

What you will practice

This scenario walks through 6 steps, in order:

  1. 01Bookmark the current tip so nothing is lost
  2. 02Rewind the branch to the fork point
  3. 03Replay the refactor first (it is backup's tip)
  4. 04Then replay the feature on top of it
  5. 05Inspect new order
  6. 06Force push reordered history
Reveal the step-by-step commands

Try the terminal first — you learn more by doing. When you want to check yourself, here is the command for each step.

  1. 1. Bookmark the current tip so nothing is lost
    git branch backup
  2. 2. Rewind the branch to the fork point
    git reset --hard HEAD~2
  3. 3. Replay the refactor first (it is backup's tip)
    git cherry-pick backup
  4. 4. Then replay the feature on top of it
    git cherry-pick backup~1
  5. 5. Inspect new order
    git log --oneline
  6. 6. Force push reordered history
    git push --force-with-lease

Key takeaway

Reordering is just replaying: a branch to hold the old tip, a hard reset back to the fork point, then cherry-picks in the new order. That is exactly what moving lines in a rebase todo does.