Reordering commits in Git
Do you need to change the order of your commits?
2 min readAug 7, 2024
It’s easier than it seems! Just do an interactive rebase and reorder the commits within the rebase, for example:
> git log --oneline
aaaa: First commit # We want this to be on the ccc commit position
bbbb: Second commit
cccc: Third commit
dddd: Other commit
The output from the previous command shows us the commits for the current branch. You can see that we want to move commit aaaa to be before commit bbbb
Do an interactive rebase of the first three commits
git rebase -i HEAD~3 # Another option > git rebase -i dddd
pick cccc: Third commit
pick bbbb: Second commit
pick aaaa: First commit
Commands:
p, pick = use commit
e, edit = use commit, but stop for amending
s, squash = use commit, but meld into previous commit
Keep the pick command and then simply re-order the lines according to the desired order, the result will be something like this:
pick aaaa: First commit # Now this will be the first commit (chronological order)
pick bbbb: Secord commit
pick cccc: Third commit
Printing out our commits again, we can see the following output:
> git log - oneline
cccc: First commit
bbbb: Second commit
aaaa: Third commit
dddd: Other commit
¡Now You have changed the commit order🎉!