Git Rebase Reorder Commits - Change the Order of Commits with git rebase -i, with Real Output for the Clean Swap, the Double Conflict, the Undo and the Abort (git 2.43.0)
TL;DR
git rebase -i <base>, then move thepicklines into the order you want. The git rebase reference says it in the comment block of the instruction list itself: these lines can be re-ordered, and they are executed from top to bottom. Nothing else is needed; no special command exists for reordering.- The list is oldest first, the opposite of
git log. Ingit log --onelinethe newest commit is on top; in the instruction list the oldest is on top. Moving a line up means "happened earlier". - Every commit from the first moved one onward gets a new hash, because its parent changed. In my run the three commits went from
438a65f,6188264,85a4525todd850f1,201f9ea,257c970. - A reorder can conflict, and it can conflict once per commit. Moving
feature abehindfeature bstopped the rebase twice in my run, witherror: could not apply 85a4525... feature band thenerror: could not apply 438a65f... feature a. - Undo a finished reorder with
git reset --hard ORIG_HEAD, or stop one in progress withgit rebase --abort. - Not your case? If you want to remove a commit instead of moving it, read git rebase drop commit; if you want to merge commits together, read git squash commits.
Table of Content
- Why reorder commits at all
- The lab repository
- Reading the instruction list
- A clean reorder
- Proving nothing else changed with range-diff
- Undoing a reorder
- When the reorder conflicts
- Giving up with git rebase --abort
- Reordering and the cost of distance
- Pushing a reordered branch
- What broke when I ran this
- Conclusion
1. Why reorder commits at all
Most of the time you do not care about the order of commits on your own branch. I reorder when one of three things is true:
| Situation | What I do | Why |
|---|---|---|
| A refactor commit landed after the feature commit that needed it | Move the refactor above the feature | Each commit builds on its own, so git bisect can test them one by one |
| A docs or config commit sits in the middle of feature work | Move it to the front or the back | The reviewer reads the feature as one block |
| A fix-up belongs next to the commit it fixes | Move it right below that commit, then change pick to fixup | Same idea as squashing commits |
The reorder is only a rewrite of your local history, and that is the one rule to keep in mind: only reorder commits nobody else has based work on. The same rule applies to dropping a commit and to amending one.
2. The lab repository
I ran everything on 2026-12-05 with git 2.43.0. The repository has four commits. Two of them (feature a and feature b) append a line to the same file, and one (add readme) creates a new file. That mix is deliberate: it gives me one reorder that is safe and one that is not.
1$ date -u +%F; git --version
22026-12-05
3git version 2.43.0
4$ git log --oneline
585a4525 feature b
66188264 add readme
7438a65f feature a
8986bac5 add app
app.txt holds the line app v1 after the first commit; feature a appends line a, feature b appends line b, and README.md only exists in the commit add readme.
3. Reading the instruction list
git rebase -i HEAD~3 replays the last three commits. Before it does, it writes an instruction list and opens your editor on it. I set GIT_SEQUENCE_EDITOR to a command so I can show the list without an editor window. This is what git wrote for the lab repository:
1pick 438a65f feature a
2pick 6188264 add readme
3pick 85a4525 feature b
4
5# Rebase 986bac5..85a4525 onto 986bac5 (3 commands)
6#
7# Commands:
8# p, pick <commit> = use commit
9# ...
10# These lines can be re-ordered; they are executed from top to bottom.
11#
(I cut the middle of the comment block; the full list of commands, such as reword, edit, squash, fixup, exec and drop, is in the git rebase reference.) Three things matter here:
- The oldest commit,
feature a, is on top.git logshowed it at the bottom. If you move a line to the top of the list, that commit becomes the oldest of the three. 986bac5..85a4525is the range. The base986bac5is the commit before the first one you want to touch;HEAD~3is how I picked it. Everything at or below the base is never replayed and keeps its hash.- Only the
picklines are executed. Lines starting with#are comments, and a blank line is ignored.
In an editor, reordering is cut and paste: move the line, save, close. For a copy-paste command that produces the same output every time, I used sed as the sequence editor in the rest of this post.
4. A clean reorder
I wanted add readme first. In the instruction list that means moving line 2 above line 1. The sed script below holds line 1, deletes it, and appends it after line 2, which swaps the first two lines:
1$ GIT_SEQUENCE_EDITOR="sed -i '1{h;d};2{G}'" git rebase -i HEAD~3
2Rebasing (1/3)Rebasing (2/3)Rebasing (3/3)Successfully rebased and updated refs/heads/main.
3$ git log --oneline
4257c970 feature b
5dd850f1 feature a
6201f9ea add readme
7986bac5 add app
Read the hashes. 986bac5, below the base, is untouched. add readme went from 6188264 to 201f9ea, feature a from 438a65f to dd850f1 and feature b from 85a4525 to 257c970. All three changed, even feature b, which did not move at all: its parent changed, and a commit hash includes the parent hash. That is the general rule, and it is why you must not do this on commits somebody else has already pulled.
The swap was clean because add readme only creates README.md, and nothing in feature a depends on that file. When the moved commits touch different files, a reorder cannot conflict.
5. Proving nothing else changed with range-diff
A reorder should change history and nothing else. Two checks I trust. First, the tree: the snapshot of the whole project at the tip must be identical before and after. I compared the tree hash of the old tip (ORIG_HEAD) with the new one in a second run of the same reorder:
1$ git diff --stat ORIG_HEAD HEAD
2$ git rev-parse ORIG_HEAD^{tree} HEAD^{tree}
380b36b983c3449448839ae8bf6d8012372b2480d
480b36b983c3449448839ae8bf6d8012372b2480d
git diff printed nothing and the two tree hashes are the same. Second, git range-diff compares the old series with the new one commit by commit. The = sign means the patch is identical, and the numbers on each side show the position:
1$ git range-diff 986bac5 ORIG_HEAD HEAD
22: 6188264 = 1: 289504b add readme
31: 438a65f = 2: fa6cc4c feature a
43: 85a4525 = 3: 20cb4f5 feature b
Reading it: add readme was commit 2 and is now commit 1, feature a was commit 1 and is now commit 2, and all three patches are equal. This is the output I paste into a pull request description when a reviewer asks "what did you change?". The hashes differ from section 4 because this was a second run of the same commands, which creates new commits with new timestamps.
6. Undoing a reorder
Git sets ORIG_HEAD to the old branch tip when the rebase starts, so right after a reorder you can go back with one command. The reference page warns that ORIG_HEAD is not guaranteed to still point at that commit if other commands that change it (like git reset) run during the rebase, so use it right away:
1$ git reset --hard ORIG_HEAD
2$ git log --oneline
385a4525 feature b
46188264 add readme
5438a65f feature a
6986bac5 add app
The original hashes are back. If you wait, the reflog has the same information. After the reorder and the reset, mine looked like this:
1$ git reflog | head -5
285a4525 HEAD@{0}: reset: moving to ORIG_HEAD
3257c970 HEAD@{1}: rebase (finish): returning to refs/heads/main
4257c970 HEAD@{2}: rebase (pick): feature b
5dd850f1 HEAD@{3}: rebase (pick): feature a
6201f9ea HEAD@{4}: rebase (pick): add readme
Every rebase (pick) line is one replayed commit, in the new order, and the old tip is a few lines further down. The git gc reference gives the default retention: reflog entries expire after 90 days (gc.reflogExpire), and after 30 days if they are no longer reachable from the current tip (gc.reflogExpireUnreachable). So a commit you lose in a reorder is recoverable for a month by default, not forever.
7. When the reorder conflicts
Now the unsafe case. feature a and feature b both append to app.txt. I moved feature a to the end of the list, so the order became add readme, feature b, feature a:
1$ GIT_SEQUENCE_EDITOR="sed -i '1{h;d};3{G}'" git rebase -i HEAD~3
2Rebasing (1/3)Rebasing (2/3)Auto-merging app.txt
3CONFLICT (content): Merge conflict in app.txt
4error: could not apply 85a4525... feature b
5hint: Resolve all conflicts manually, mark them as resolved with
6hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
7hint: You can instead skip this commit: run "git rebase --skip".
8hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
9Could not apply 85a4525... feature b
The commit that failed is feature b, not the one I moved. That makes sense once you think about it: feature b was written on top of a file that contained line a, and now it is applied to a file that does not. git status says where I am in the list:
1$ git status | head -8
2interactive rebase in progress; onto 986bac5
3Last commands done (2 commands done):
4 pick 6188264 add readme
5 pick 85a4525 feature b
6Next command to do (1 remaining command):
7 pick 438a65f feature a
8 (use "git rebase --edit-todo" to view and edit)
9You are currently rebasing branch 'main' on '986bac5'.
And git diff shows the conflict markers, with HEAD being the new history so far and the other side being the commit being applied:
1$ git diff
2diff --cc app.txt
3@@@ -1,1 -1,3 +1,6 @@@
4 app v1
5++<<<<<<< HEAD
6++=======
7+ line a
8+ line b
9++>>>>>>> 85a4525 (feature b)
The conflict is "the commit I am applying expects line a to exist". I resolved it by writing the file as app v1 plus line b, then git add app.txt and git rebase --continue (with GIT_EDITOR=true so no editor opened). It stopped again, at the next commit:
1$ git add app.txt && GIT_EDITOR=true git rebase --continue
2error: could not apply 438a65f... feature a
3hint: Resolve all conflicts manually, mark them as resolved with
4hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
5$ cat app.txt
6app v1
7<<<<<<< HEAD
8line b
9=======
10line a
11>>>>>>> 438a65f (feature a)
Two lessons from that. A reorder can stop once per commit it moves or that depends on a moved one, so budget for that. And the final content is yours to decide, which is the dangerous part: I resolved the second conflict as line b then line a, and then compared with the old tip:
1$ git diff ORIG_HEAD HEAD
2--- a/app.txt
3+++ b/app.txt
4@@ -1,3 +1,3 @@
5 app v1
6-line a
7 line b
8+line a
The tree is not the same as before. The reorder changed the file, because the two lines now appear in the new order. That is correct for my resolution, but it is exactly the check I would run before pushing: if the intent was "same result, different history", git diff ORIG_HEAD HEAD has to print nothing. I turned on git config rerere.enabled true afterwards so git can remember a resolution; the git rerere reference describes how it replays recorded conflict resolutions, but I did not run a second conflict to show it.
8. Giving up with git rebase --abort
The way out of a conflicting reorder is git rebase --abort. It returns the branch to its state before the rebase started. I started the same conflicting reorder again and aborted:
1$ git rebase --abort
2$ git status | head -2
3On branch main
4nothing to commit, working tree clean
5$ git log --oneline | head -2
685a4525 feature b
76188264 add readme
8$ git rev-parse HEAD
985a45255829a5f82f262415af58581999479c004
The tip is 85a4525 again, the original hash. The other options are git rebase --skip (throws away the commit that failed, which in a reorder means losing work) and the edit option shown in the status output above (it reopens the remaining list for changes). Both are described in the git rebase reference. I use --abort almost every time and instead reorder in smaller steps, one move per rebase.
9. Reordering and the cost of distance
Every commit from the first moved one up to the tip is replayed, so the base you pick decides the runtime. I built a repository with 1,001 commits, where each commit adds its own file, and swapped two commits 100 commits below the tip with git rebase -i HEAD~100:
1$ time (GIT_SEQUENCE_EDITOR="sed -i '1{h;d};2{G}'" git rebase -i HEAD~100 >/dev/null 2>&1)
2real 0m7.147s
3$ git rev-list --count HEAD
41001
That is 7.1 seconds for 100 replayed commits on git 2.43.0 in a repository of tiny files, or roughly 70 ms per commit, and the branch still had 1,001 commits afterwards. Real projects with bigger trees and hooks will be slower; at the same 70 ms per commit, 500 replayed commits would take about 35 seconds, which is an estimate from my run, not a measurement. Pick the base as close to the commits you move as you can. I did not run the same reorder with HEAD~1000, so I have no number for it.
| Part of history | Replayed | New hash |
|---|---|---|
| At or below the base | No | No |
| Between the base and the first moved commit | Yes | Yes (only if the instruction list starts there) |
| Everything from the first changed line to the tip | Yes | Yes |
In my 3-commit run, the base 986bac5 kept its hash and all three commits above it changed.
10. Pushing a reordered branch
If the branch exists on the remote, the reordered branch has different hashes for commits the remote already has, so a plain git push is rejected as a non-fast-forward. That rejection is the proof that you rewrote history, not a bug. The git push reference describes --force-with-lease, which refuses to overwrite the remote branch if someone pushed to it since you last fetched. I use that on a branch that is mine and never on main:
1git push --force-with-lease origin my-branch
I did not push in this lab, because the repository has no remote; the rejection text for exactly this case is in my post on dropping a commit, where I did run it.
11. What broke when I ran this
A half-finished rebase after I looked at the list. To print the instruction list in section 3, I ran GIT_SEQUENCE_EDITOR=cat git rebase -i HEAD~3 | head -30. That printed the list, but the rebase was left stopped in the middle, and the next reorder attempt failed with:
1fatal: It seems that there is already a rebase-merge directory, and
2I wonder if you are in the middle of another rebase. If that is the
3case, please try
4 git rebase (--continue | --abort | --skip)
git status said You are currently editing a commit while rebasing branch 'main' on '986bac5'. I believe the closed pipe from head killed git partway through, but I did not confirm the cause. The fix was git rebase --abort, and the lesson is to never pipe an interactive rebase into head. To read the list without risk, run git rebase -i in the editor and close it without saving, or use git log --reverse --oneline <base>..HEAD, which prints the same order.
I moved the wrong commit and the error named another one. In the conflicting reorder, git reported could not apply 85a4525... feature b, but the line I had moved was feature a. The commit git names is the one it was applying when it failed, not the one you moved. Read the "Last commands done" block in git status to see where the list stopped.
The result was not the same tree. When I resolved the two conflicts, the final app.txt had the lines in a different order than before (section 7). Git did not warn me, because for git the resolution was valid. git diff ORIG_HEAD HEAD is the check.
What I did not test: reordering across a merge commit. A plain interactive rebase flattens merges unless you pass --rebase-merges, which deserves its own post.
12. Conclusion
git rebase -i <base>and move thepicklines. The list runs top to bottom and is oldest first.- Every commit from the first changed line to the tip gets a new hash; the base and everything below it keep theirs.
- Commits that touch different files swap cleanly; commits that edit the same lines conflict, and git names the commit it was applying, not the one you moved.
- After a reorder,
git diff ORIG_HEAD HEADprinting nothing andgit range-diff <base> ORIG_HEAD HEADshowing only=lines proves that history changed and the content did not. git reset --hard ORIG_HEADundoes a finished reorder;git rebase --abortstops one in progress.- Push a reordered branch with
--force-with-lease, and only if it is yours.
This is Day 10 of the DevOps Master Course on YouTube, and the written foundation is in the free Git and GitHub blueprint course. To test every commit after a reorder, see git rebase --exec; to see what a diff between the old and new history looks like, read git diff.
Did this save you a debugging session? Stay in the loop -
- Subscribe on YouTube - every post here has a screen-recorded companion video.
- Code on GitHub - the repositories, Terraform and scripts behind these posts.