Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to re-order commits in Git non-interactively

What non-interactive git command(s) achieve the change from Before to After?

Before:

A---B---C---D 

After:

A---C'---B'---D' 
like image 221
James Tauber Avatar asked Feb 12 '11 22:02

James Tauber


People also ask

Can you amend a previous commit?

The git commit --amend command is a convenient way to modify the most recent commit. It lets you combine staged changes with the previous commit instead of creating an entirely new commit. It can also be used to simply edit the previous commit message without changing its snapshot.

Can we revert the commit in git?

The git revert command is used for undoing changes to a repository's commit history. Other 'undo' commands like, git checkout and git reset , move the HEAD and branch ref pointers to a specified commit. Git revert also takes a specified commit, however, git revert does not move ref pointers to this commit.


2 Answers

In your case, you can rebase interactive: git rebase -i HEAD~4 Then you can just reorder your picks

For example lets add three more files to our branch:

git add A git commit -m "A"  git add B git commit -m "B"  git add C git commit -m "C" 

Your shortlog will be:

$ git shortlog  (3):       A       B       C 

If you want to reorder B with C:

$ git rebase -i HEAD~2 pick 1f9133d B pick 33f41be C 

You just re-order them to be:

pick 33f41be C pick 1f9133d B 

After you're done writing, see the shortlog:

$ git shortlog  (3):       A       C       B 

You can do the same thing with all the commits by re-ordering. It is like what you see is what you get, which is pretty cool :)

like image 87
Mohamed Mansour Avatar answered Oct 31 '22 20:10

Mohamed Mansour


Try this:

git reset --hard A git cherry-pick C git cherry-pick B git cherry-pick D 

There may be a way with git rebase, but I didn't really understand it.

like image 38
Paŭlo Ebermann Avatar answered Oct 31 '22 18:10

Paŭlo Ebermann