threads / discuss / 29979

How to reorder all commits include the initial commit

Subject: How to reorder all commits include the initial commit

## tl;dr

6 messages between Mar 17, 2012 and Mar 18, 2012.

replies: 5people: 4as markdown or json

이응준· Mar 17, 2012, 13:15 UTC · lore

Sometimes, I need to reorder all commits, which include the initial commit, in my branch. So I tried it using git-rebase as follows, but it failed with the fatal error.

(supposing the initial commit is 793ea88) $ git rebase -i 793ea88^ fatal: Needed a single revision invalid upstream 793ea88^

How can I do that?
David Barr· Mar 17, 2012, 14:06 UTC · re: 이응준 · lore

Re: How to reorder all commits include the initial commit

On Sun, Mar 18, 2012 at 12:15 AM, 이응준 <semtlenori@gmail.com> wrote:
Show 10 quoted lines
> Sometimes, I need to reorder all commits, which include the initial
> commit, in my branch.
> So I tried it using git-rebase as follows, but it failed with the fatal error.
>
> (supposing the initial commit is 793ea88)
> $ git rebase -i 793ea88^
> fatal: Needed a single revision
> invalid upstream 793ea88^
>
> How can I do that?

From my reading of git-rebase(1), this is currently unsupported. However, I was able to reorder linear history including the root using git format-patch and git am.

  git init test
  cd test/
  echo a>b
  git add b
  git commit -m '1'
  echo c>d
  git add d
  git commit -m '2'
  git init new
  cd new/
  git am ../0002-2.patch
  git am ../0001-1.patch

-- David Barr

Jakub Narebski· Mar 17, 2012, 14:26 UTC · re: 이응준 · lore

Re: How to reorder all commits include the initial commit

semtlenori@gmail.com writes:
Show 10 quoted lines
> Sometimes, I need to reorder all commits, which include the initial
> commit, in my branch.
> So I tried it using git-rebase as follows, but it failed with the fatal error.
> 
> (supposing the initial commit is 793ea88)
> $ git rebase -i 793ea88^
> fatal: Needed a single revision
> invalid upstream 793ea88^
> 
> How can I do that?
Use `--root` option.
-- 
Jakub Narebski
Yi, EungJun· Mar 17, 2012, 19:45 UTC · re: Jakub Narebski · lore

Re: How to reorder all commits include the initial commit

Thanks to David Barr and Jakub Narebski. Finally I found the way based on your hints to solve my problem.

$ git checkout -b test --orphan $ git format-patch <newroot>^..<newroot> | git am $ git rebase -i --onto test --root master $ git branch -M test master

Please let me know if there is more simple way.
2012년 3월 17일 오후 11:26, Jakub Narebski <jnareb@gmail.com>님의 말:
Show 18 quoted lines
> semtlenori@gmail.com writes:
>
>> Sometimes, I need to reorder all commits, which include the initial
>> commit, in my branch.
>> So I tried it using git-rebase as follows, but it failed with the fatal error.
>>
>> (supposing the initial commit is 793ea88)
>> $ git rebase -i 793ea88^
>> fatal: Needed a single revision
>> invalid upstream 793ea88^
>>
>> How can I do that?
>
> Use `--root` option.
>
> --
> Jakub Narebski
>
Jiang Xin· Mar 18, 2012, 02:58 UTC · re: Yi, EungJun · lore

Re: How to reorder all commits include the initial commit

2012/3/18 Yi, EungJun <semtlenori@gmail.com>:
> Thanks to David Barr and Jakub Narebski.
> Finally I found the way based on your hints to solve my problem.
>
> $ git checkout -b test --orphan
option -b and --orphan can not be used together.
> $ git format-patch <newroot>^..<newroot> | git am
can not apply patch when there is no root commit
My test for your case:
log of master branch before rebase:
    $ git log --oneline
    1a91ece commit 3
    d7c6dec commit 2
    e32542d commit 1
rebase:
    $ git checkout --orphan new-master
    $ rm .git/index
    $ git commit --allow-empty -m initial
    $ git clean -fd
    $ git cherry-pick -n master^
    $ git commit --amend -C master^
    $ git rebase --root --onto new-master master
    $ git branch -d new-master
master after rebase:
    $ git log --oneline
    fbb9fb6 commit 3
    56afd5c commit 1
    894c42a commit 2

-- Jiang Xin

Yi, EungJun· Mar 18, 2012, 19:05 UTC · re: Jiang Xin · lore

Re: How to reorder all commits include the initial commit

2012년 3월 18일 오전 11:58, Jiang Xin <worldhello.net@gmail.com>님의 말:
Show 8 quoted lines
>> $ git checkout -b test --orphan
>
> option -b and --orphan can not be used together.
>
>> $ git format-patch <newroot>^..<newroot> | git am
>
> can not apply patch when there is no root commit
>
You are all right. I made several mistakes including what you point out.
I fixed them as follows:

$ git checkout --orphan test $ git rm '*' -f $ git show <newroot> | git apply - --index $ git commit --reuse-message <newroot> $ git rebase --onto test --root master $ git branch -d test

← back to recent threads