Reordering and Splitting Commits

A real, unrelated fix accidentally rode along in the wrong commit. Split it out, then reorder two others.

A real, honest mistake: while adding the dinner recipe, a small typo fix to breakfast.md got staged and committed right alongside it. One real commit, two genuinely unrelated changes — exactly the kind of thing a careful reviewer would flag.\n\ngit rebase -i's edit verb pauses the rebase directly at that commit, with its changes already applied and staged. From there, git reset HEAD~1 genuinely un-commits it — without touching the actual file changes — leaving them staged and ready to be split into two real, focused commits before the rebase continues.

Finding the Real Mixed-Up Commit

git show <hash> --stat

Two genuinely unrelated files changed in one real commit — a clear signal it should be two commits, not one.

Pausing Mid-Rebase to Split It

GIT_SEQUENCE_EDITOR="sed -i '3s/^pick/edit/'" git rebase -i HEAD~5
git reset HEAD~1

The real edit verb stops the rebase exactly at that commit; git reset HEAD~1 genuinely un-commits it while leaving every real file change staged and ready to split.

Two Real, Focused Commits

git add dinner.md
git commit -m "Add dinner recipe"
git add breakfast.md
git commit -m "Fix typo in breakfast recipe"
git rebase --continue

Reordering Two Independent Commits

GIT_SEQUENCE_EDITOR="sed -i '1{h;d};2{G}'" git rebase -i HEAD~2

A real, scripted swap of the two todo lines — since dessert and snack touch genuinely different files, the reorder replays cleanly with no real conflict.

edit (in an interactive rebase)

Pauses the rebase at a specific real commit, with its changes already applied to the working tree. Confirmed directly in this lab: this is the real starting point for amending, inspecting, or splitting that exact commit before continuing.

git reset HEAD~1 (mid-rebase)

Genuinely un-commits the paused commit while leaving its real file changes staged and ready to work with. Confirmed directly in this lab: this is how one real commit becomes two, without losing or redoing any actual change.

🌱 Create the Real Mixed-Up History

A real, honest mistake: an unrelated fix rides along in the wrong commit.

cd ~/team-recipes && git switch master
cd ~/team-recipes && git switch -c feature/meal-recipes
cd ~/team-recipes && printf 'Breakfast\n\nOatmeal with fruit.\n' > breakfast.md
cd ~/team-recipes && git add breakfast.md
cd ~/team-recipes && git commit -m 'Add breakfast recipe'
cd ~/team-recipes && printf 'Lunch\n\nGrilled cheese sandwich.\n' > lunch.md
cd ~/team-recipes && git add lunch.md
cd ~/team-recipes && git commit -m 'Add lunch recipe'
cd ~/team-recipes && printf 'Dinner\n\nBaked salmon with vegetables.\n' > dinner.md
cd ~/team-recipes && sed -i 's/Oatmeal with fruit\./Oatmeal with fruit and honey./' breakfast.md
cd ~/team-recipes && git add dinner.md breakfast.md
cd ~/team-recipes && git commit -m 'Add dinner recipe'
cd ~/team-recipes && printf 'Dessert\n\nApple pie.\n' > dessert.md
cd ~/team-recipes && git add dessert.md
cd ~/team-recipes && git commit -m 'Add dessert recipe'
cd ~/team-recipes && printf 'Snack\n\nTrail mix.\n' > snack.md
cd ~/team-recipes && git add snack.md
cd ~/team-recipes && git commit -m 'Add snack recipe'
cd ~/team-recipes && git log --oneline -5

student@lab:~$ cd ~/team-recipes && git switch master Switched to branch 'master' Your branch is up to date with 'origin/master'. student@lab:~$ cd ~/team-recipes && git switch -c feature/meal-recipes Switched to a new branch 'feature/meal-recipes' student@lab:~$ cd ~/team-recipes && printf 'Breakfast\n\nOatmeal with fruit.\n' > breakfast.md student@lab:~$ cd ~/team-recipes && git add breakfast.md student@lab:~$ cd ~/team-recipes && git commit -m 'Add breakfast recipe' [feature/meal-recipes 07ad7e1] Add breakfast recipe 1 file changed, 3 insertions(+) create mode 100644 breakfast.md student@lab:~$ cd ~/team-recipes && printf 'Lunch\n\nGrilled cheese sandwich.\n' > lunch.md student@lab:~$ cd ~/team-recipes && git add lunch.md student@lab:~$ cd ~/team-recipes && git commit -m 'Add lunch recipe' [feature/meal-recipes 51e5a9f] Add lunch recipe 1 file changed, 3 insertions(+) create mode 100644 lunch.md student@lab:~$ cd ~/team-recipes && printf 'Dinner\n\nBaked salmon with vegetables.\n' > dinner.md student@lab:~$ cd ~/team-recipes && sed -i 's/Oatmeal with fruit\./Oatmeal with fruit and honey./' breakfast.md student@lab:~$ cd ~/team-recipes && git add dinner.md breakfast.md student@lab:~$ cd ~/team-recipes && git commit -m 'Add dinner recipe' [feature/meal-recipes d1cf302] Add dinner recipe 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 dinner.md student@lab:~$ cd ~/team-recipes && printf 'Dessert\n\nApple pie.\n' > dessert.md student@lab:~$ cd ~/team-recipes && git add dessert.md student@lab:~$ cd ~/team-recipes && git commit -m 'Add dessert recipe' [feature/meal-recipes 0d4cc55] Add dessert recipe 1 file changed, 3 insertions(+) create mode 100644 dessert.md student@lab:~$ cd ~/team-recipes && printf 'Snack\n\nTrail mix.\n' > snack.md student@lab:~$ cd ~/team-recipes && git add snack.md student@lab:~$ cd ~/team-recipes && git commit -m 'Add snack recipe' [feature/meal-recipes 53a6773] Add snack recipe 1 file changed, 3 insertions(+) create mode 100644 snack.md student@lab:~$ cd ~/team-recipes && git log --oneline -5 53a6773 Add snack recipe 0d4cc55 Add dessert recipe d1cf302 Add dinner recipe 51e5a9f Add lunch recipe 07ad7e1 Add breakfast recipe

🔍 Confirm the Real Mix-Up

See exactly which two unrelated files landed in the same commit.

cd ~/team-recipes && git show --stat $(git log --oneline -5 | sed -n '3p' | cut -d' ' -f1)

student@lab:~$ cd ~/team-recipes && git show --stat $(git log --oneline -5 | sed -n '3p' | cut -d' ' -f1) commit d1cf3029f6aeaf55d728060e97f7dcf3e0593a91 Author: Ada Lovelace <ada@example.com> Date: Thu Aug 27 20:13:50 2026 +0200 Add dinner recipe breakfast.md | 2 +- dinner.md | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-)

✂️ Split It Into Two Real, Focused Commits

Pause the rebase, un-commit, then recommit each real change on its own.

cd ~/team-recipes && GIT_SEQUENCE_EDITOR="sed -i '3s/^pick/edit/'" git rebase -i HEAD~5
cd ~/team-recipes && git reset HEAD~1
cd ~/team-recipes && git add dinner.md
cd ~/team-recipes && git commit -m 'Add dinner recipe'
cd ~/team-recipes && git add breakfast.md
cd ~/team-recipes && git commit -m 'Fix typo in breakfast recipe'
cd ~/team-recipes && git rebase --continue
cd ~/team-recipes && git log --oneline -6

student@lab:~$ cd ~/team-recipes && GIT_SEQUENCE_EDITOR="sed -i '3s/^pick/edit/'" git rebase -i HEAD~5 Stopped at d1cf302... Add dinner recipe You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue student@lab:~$ cd ~/team-recipes && git reset HEAD~1 Unstaged changes after reset: M breakfast.md student@lab:~$ cd ~/team-recipes && git add dinner.md student@lab:~$ cd ~/team-recipes && git commit -m 'Add dinner recipe' [detached HEAD b811ba7] Add dinner recipe 1 file changed, 3 insertions(+) create mode 100644 dinner.md student@lab:~$ cd ~/team-recipes && git add breakfast.md student@lab:~$ cd ~/team-recipes && git commit -m 'Fix typo in breakfast recipe' [detached HEAD 234bfef] Fix typo in breakfast recipe 1 file changed, 1 insertion(+), 1 deletion(-) student@lab:~$ cd ~/team-recipes && git rebase --continue Successfully rebased and updated refs/heads/feature/meal-recipes. student@lab:~$ cd ~/team-recipes && git log --oneline -6 1aa36d7 Add snack recipe d6d996a Add dessert recipe 234bfef Fix typo in breakfast recipe b811ba7 Add dinner recipe 51e5a9f Add lunch recipe 07ad7e1 Add breakfast recipe

🔀 Reorder Two Independent Commits

Dessert and snack touch genuinely different files — swap their real order safely.

cd ~/team-recipes && GIT_SEQUENCE_EDITOR="sed -i '1{h;d};2{G}'" git rebase -i HEAD~2
cd ~/team-recipes && git log --oneline -2
cd ~/team-recipes && git show --stat HEAD~1
cd ~/team-recipes && git show --stat HEAD

student@lab:~$ cd ~/team-recipes && GIT_SEQUENCE_EDITOR="sed -i '1{h;d};2{G}'" git rebase -i HEAD~2 Successfully rebased and updated refs/heads/feature/meal-recipes. student@lab:~$ cd ~/team-recipes && git log --oneline -2 32e4f38 (HEAD -> feature/meal-recipes) Add dessert recipe 3e98ed1 Add snack recipe student@lab:~$ cd ~/team-recipes && git show --stat HEAD~1 commit 3e98ed1a7f3d046f704a8f96a0f4c2abc33f20fd Author: Ada Lovelace <ada@example.com> Date: Thu Aug 27 20:13:50 2026 +0200 Add snack recipe snack.md | 3 +++ 1 file changed, 3 insertions(+) student@lab:~$ cd ~/team-recipes && git show --stat HEAD commit 32e4f388d577a40918a687de2ee80dbd21676a08 (HEAD -> feature/meal-recipes) Author: Ada Lovelace <ada@example.com> Date: Thu Aug 27 20:13:50 2026 +0200 Add dessert recipe dessert.md | 3 +++ 1 file changed, 3 insertions(+)

Lab 3.1.2 complete. A real mixed-up commit, split apart — then two independent commits, reordered:\n\n\n Real mix-up confirmed : ✅ git show --stat\n Split into two commits : ✅ edit + reset HEAD~1\n Reordered safely : ✅ scripted todo swap\n

Enable JavaScript to run the live terminal and track your progress.