88

I want to reorder last two commits in git:

right now I have:

$ git log --oneline -4
1e0ecba (HEAD, my-branch) Fix for T255
82d45dc django_extensions
af3953b improvements according to CR
dae63ff Fullscreen support

I want to have:

$ git log --oneline -4
82d45dc (HEAD, my-branch) django_extensions
1e0ecba Fix for T255
af3953b improvements according to CR
dae63ff Fullscreen support
0

4 Answers 4

162

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.

4
53

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.
6
  • 35
    I'd personally stick with just a plain vanilla interactive rebase. Commented Oct 28, 2015 at 10:15
  • 2
    @TimBiegeleisen I needed this alias because I like to keep some personal adjustments to code in my last commit. During development, when I create new commits I need to reorder my-special-commit to keep it as last... and If I want to push this, I do: git push origin HEAD~1:my-branch
    – noisy
    Commented Oct 28, 2015 at 10:23
  • 4
    To make it work on macos systems, go with "sed -i '' -n" instead of "sed -i -n".
    – ecmanaut
    Commented Jul 12, 2017 at 12:24
  • 1
    @noisy In that case, I would prefer to commit your local changes to a local branch, and then just merge the public development into your adjusted branch. Make changes while you are on your branch, then, when you are ready to commit, checkout the last public commit, commit your changes, get back to your local branch, and merge your new commit. That way you get a full locally working history, never have to rebase, and public history stays clean of your local changes. Commented Mar 23, 2018 at 15:23
  • 1
    Very nice, exactly what I was looking for, thank you. If anyone is wondering, in case of conflicts between the two commits, rebase will apply the first commit (originally HEAD), and then stay in conflict state waiting for you to: skip commit, fix conflicts or abort rebase.
    – Marko
    Commented Nov 17, 2022 at 16:08
11

Assuming no conflicts:

  • git rebase -i HEAD~4
  • Re-order commits + save
0

A little late to the party, but I have written the following answer from the standpoint of understanding how few git commands work. The other answers will get your work done in a shorter way with the help of interactive rebase commands. I had encountered this similar situation and this is how I solved my problem -

Suppose this is how your last 4 commits look like.

* ca6807e - (HEAD -> lolo) third (4 seconds ago) <Kaustubh Butte>
| 
* 61a069c - (master) fourth (18 hours ago) <Kaustubh Butte>
| 
* f3b0255 - (lol) second (2 days ago) <Kaustubh Butte>
| 
* c2f5e4f - first (2 days ago) <Kaustubh Butte>

Now according to your question you want to make the commit history as follows (Ignore the rest of the commit ids)-

* 0fe6482 - (HEAD -> master) fourth (5 seconds ago) <Kaustubh Butte>
| 
* 2c3ba40 - (lol) third (4 minutes ago) <Kaustubh Butte>
| 
* f3b0255 - (lolo) second (2 days ago) <Kaustubh Butte>
| 
* c2f5e4f - first (2 days ago) <Kaustubh Butte>

To do that follow these steps -

  1. Go to the 3rd last commit

    git reset HEAD~2

    Now your HEAD will point at the 'second' commit

  2. At this point, we will switch our branch from master to lol (look at the first tree diagram)-

    git checkout lol

    Now if you do git status you may find some files are in the untracked section that is because you checked back to a previous commit in the history which which did not have those files in tracking mode but your remote repository has them. So you have to just ignore those files for now. But git will not let you move forward with our approach if we keep those files untracked. So we add all of them by doing git add * followed by git stash which will save those files in a temporary stack (ignoring them temporarily and then allowing us to pop those files from this stack later when we need).

  3. Now we need to move our 'fourth' commit on the current branch 'lol'. To do that we need to use git cherry-pick command. Copy the commit id of the 'fourth' commit which is ca6807e in my case. Now type git cherry-pick ca6807e. Now the tree structure will look something like this -

    * 2c3ba40 - (HEAD -> lol) third (3 seconds ago) <Kaustubh Butte>
    |   
    | * 61a069c - (master) fourth (18 hours ago) <Kaustubh Butte>
    |/  
    | 
    * f3b0255 - (lolo) second (2 days ago) <Kaustubh Butte>
    | 
    * c2f5e4f - first (2 days ago) <Kaustubh Butte>
    

Now you just need to do switch back to master branch and rebase lol branch on it.

git checkout master
git rebase lol

And now you have successfully interchanged the last two commits. Here is how the tree structure looks like now -

* 0fe6482 - (HEAD -> master) fourth (5 seconds ago) <Kaustubh Butte>
| 
* 2c3ba40 - (lol) third (4 minutes ago) <Kaustubh Butte>
| 
* f3b0255 - (lolo) second (2 days ago) <Kaustubh Butte>
| 
* c2f5e4f - first (2 days ago) <Kaustubh Butte>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.