How to reorder last two commits in git?

In general you have to use git rebase --interactive - here is detail answer how to reorder any number of commits:

But if you want to reorder last two commits you can use this git alias:

Add to ~/.gitconfig:

[alias]
    reorder = "!GIT_SEQUENCE_EDITOR=\"sed -i -n 'h;1n;2p;g;p'\" git rebase -i HEAD~2"

and then:

$ git reorder
Rebasing(2/2)
Successfully rebased and updated refs/heads/my-branch.

I truly recommend to read this answer about how to reorder commits, you'll feel like a git rockstar afterwards, I promise.


Apart from that, here's how you do it with a simple rebase (assuming you're standing on the branch you want to rebase):

git rebase -i HEAD~2

Next, change the order of the commits in the prompt.

pick f4648aee My first commit
pick 00adf09a My second commit

to

pick 00adf09a My second commit
pick f4648aee My first commit

Unbelievable that it can be that simple, if you ask me.

Tags:

Git