Bird
Raised Fist0
Gitdevops~10 mins

Reordering commits in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Reordering commits
Start: Identify commits to reorder
Run interactive rebase
Editor opens with commit list
Change order of commits
Save and close editor
Git applies commits in new order
Rebase complete with reordered commits
The flow shows how to reorder commits by starting an interactive rebase, editing the commit order, and applying the changes.
Execution Sample
Git
git rebase -i HEAD~3
# In editor, reorder commits lines
# Save and exit
# Git reapplies commits in new order
This code starts an interactive rebase for the last 3 commits, lets you reorder them, then applies the new order.
Process Table
StepActionDetailsResult
1Run interactive rebasegit rebase -i HEAD~3Editor opens with last 3 commits listed
2Edit commit orderChange lines order in editorCommit order changed in editor buffer
3Save and exit editorSave file and close editorGit starts replaying commits in new order
4Apply first commitGit applies commit originally 3rdFirst commit applied successfully
5Apply second commitGit applies commit originally 1stSecond commit applied successfully
6Apply third commitGit applies commit originally 2ndThird commit applied successfully
7Rebase completeAll commits applied in new orderBranch history reordered
💡 All commits reapplied in the new order, rebase finished successfully
Status Tracker
VariableStartAfter Step 2After Step 6Final
commit_order[C1, C2, C3][C3, C1, C2][C3, C1, C2][C3, C1, C2]
Key Moments - 3 Insights
Why does the editor show commits in chronological order?
The editor lists commits from oldest to newest because Git replays commits starting from the oldest to the newest after you reorder them (see execution_table steps 2 and 4).
What happens if there is a conflict during rebase?
Git pauses rebase and asks you to resolve conflicts manually before continuing. This is not shown here but would interrupt the flow after step 4 or 5.
Does reordering commits change the commit content?
No, reordering only changes the sequence commits are applied, not their content (see variable_tracker where commit content stays the same).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the commit order after step 2?
A[C2, C3, C1]
B[C3, C1, C2]
C[C1, C2, C3]
D[C2, C1, C3]
💡 Hint
Check the 'Details' column in step 2 where the commit order is changed in the editor.
At which step does Git start applying commits in the new order?
AStep 4
BStep 2
CStep 3
DStep 7
💡 Hint
Look at the 'Result' column in step 4 where the first commit is applied after reordering.
If you reorder commits incorrectly, what will happen during rebase?
ANothing changes, commits stay in original order
BGit will automatically fix the order
CGit will pause and ask to resolve conflicts
DGit deletes commits
💡 Hint
Refer to key_moments about conflicts during rebase.
Concept Snapshot
Reordering commits with git rebase -i HEAD~N
- Opens editor with last N commits
- Change order by moving lines
- Save and close to apply new order
- Git reapplies commits in new sequence
- Conflicts may pause rebase for manual fix
Full Transcript
Reordering commits in Git involves using interactive rebase. You start by running 'git rebase -i HEAD~3' to edit the last three commits. An editor opens showing commits from oldest to newest. You reorder the lines to change commit sequence. After saving and closing the editor, Git applies commits in the new order. The process finishes when all commits are reapplied. If conflicts occur, Git pauses for manual resolution. The commit content does not change, only their order in history.

Practice

(1/5)
1. What is the main purpose of using git rebase -i when working with commits?
easy
A. To create a new branch from the current commit
B. To reorder, edit, or squash commits interactively
C. To merge two branches automatically
D. To delete the entire commit history

Solution

  1. Step 1: Understand the function of git rebase -i

    This command opens an interactive editor allowing you to reorder, edit, or squash commits.
  2. Step 2: Compare with other options

    Creating branches, merging, or deleting history are not the main purposes of this command.
  3. Final Answer:

    To reorder, edit, or squash commits interactively -> Option B
  4. Quick Check:

    Interactive rebase = reorder commits [OK]
Hint: Use interactive rebase to reorder commits easily [OK]
Common Mistakes:
  • Confusing rebase with branch creation
  • Thinking rebase deletes history
  • Mixing rebase with merge commands
2. Which of the following is the correct command to start an interactive rebase for the last 3 commits?
easy
A. git rebase -i HEAD~3
B. git rebase -i HEAD^3
C. git rebase -i HEAD~
D. git rebase -i HEAD^^3

Solution

  1. Step 1: Recall the syntax for interactive rebase

    The correct syntax uses HEAD~N to specify the last N commits, so HEAD~3 means last 3 commits.
  2. Step 2: Check other options for syntax errors

    HEAD^3 and HEAD^^3 are invalid for this purpose; HEAD~ is incomplete.
  3. Final Answer:

    git rebase -i HEAD~3 -> Option A
  4. Quick Check:

    Use HEAD~N for last N commits [OK]
Hint: Use HEAD~N to specify last N commits in rebase [OK]
Common Mistakes:
  • Using caret (^) incorrectly for commit range
  • Omitting the number after ~
  • Confusing HEAD~ with HEAD^
3. Given the following interactive rebase todo list:
pick a1b2c3 Commit A
pick d4e5f6 Commit B
pick 789abc Commit C

If you change it to:
pick d4e5f6 Commit B
pick a1b2c3 Commit A
pick 789abc Commit C

What will be the order of commits after the rebase?
medium
A. Commit B, Commit A, Commit C
B. Commit A, Commit B, Commit C
C. Commit C, Commit B, Commit A
D. Commit A, Commit C, Commit B

Solution

  1. Step 1: Understand the todo list order

    The interactive rebase applies commits in the order listed. Changing the order changes commit history order.
  2. Step 2: Apply the new order

    New order is Commit B, then Commit A, then Commit C.
  3. Final Answer:

    Commit B, Commit A, Commit C -> Option A
  4. Quick Check:

    Rebase applies commits in listed order [OK]
Hint: Order commits in todo list to reorder history [OK]
Common Mistakes:
  • Assuming original order stays after rebase
  • Confusing commit hashes with order
  • Ignoring the todo list sequence
4. You ran git rebase -i HEAD~3 and changed the order of commits, but Git shows a conflict error. What is the best way to fix this?
medium
A. Force push the branch without resolving conflicts
B. Abort the rebase with git rebase --abort and try again
C. Delete the .git folder and start over
D. Manually resolve conflicts, then run git rebase --continue

Solution

  1. Step 1: Understand rebase conflict handling

    When conflicts occur during rebase, you must manually fix them in files.
  2. Step 2: Continue rebase after resolving conflicts

    After fixing conflicts, run git rebase --continue to proceed.
  3. Final Answer:

    Manually resolve conflicts, then run git rebase --continue -> Option D
  4. Quick Check:

    Fix conflicts + git rebase --continue [OK]
Hint: Fix conflicts, then continue rebase with git rebase --continue [OK]
Common Mistakes:
  • Ignoring conflicts and force pushing
  • Aborting rebase without trying to fix
  • Deleting .git folder which loses repo data
5. You want to reorder the last 4 commits to put the oldest commit last without changing their content. Which sequence of commands correctly achieves this?
hard
A. Run git reset --hard HEAD~4 then cherry-pick commits in new order
B. Run git merge --reorder HEAD~4 to reorder commits automatically
C. Run git rebase -i HEAD~4, then move the first commit line to the bottom in the editor
D. Run git rebase -i HEAD~3 and swap the last two commits

Solution

  1. Step 1: Use interactive rebase on last 4 commits

    Running git rebase -i HEAD~4 opens the last 4 commits for editing order.
  2. Step 2: Move the oldest commit line to the bottom

    In the editor, reorder lines so the oldest commit is last, preserving content.
  3. Final Answer:

    Run git rebase -i HEAD~4, then move the first commit line to the bottom in the editor -> Option C
  4. Quick Check:

    Interactive rebase + reorder lines = reorder commits [OK]
Hint: Use interactive rebase and reorder lines to change commit order [OK]
Common Mistakes:
  • Using reset which loses commit history
  • Trying non-existent git merge --reorder command
  • Using wrong commit range in rebase