Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reorder last two commits in git?

Tags:

git

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 
like image 806
noisy Avatar asked Oct 28 '15 10:10

noisy


People also ask

How do I change the last two commits?

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.

Can you reorder git commits?

Interactive Rebase also allows you to reorder commits. Simply drag and drop a commit between two existing commits to reorder history.

How do I reorder commits in a branch?

SourceTree makes reordering commits really easy. Right click on the last commit of the remote branch (origin/master for example), and choose “rebase children of <hash> interactively…” from the context menu. A dialog will appear with a list of the commits that are above the one you selected.


2 Answers

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.

like image 182
Jim Aho Avatar answered Oct 02 '22 15:10

Jim Aho


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. 
like image 21
noisy Avatar answered Oct 02 '22 15:10

noisy