# How to reorder all commits include the initial commit

6 messages from 2012-03-17 to 2012-03-18. Participants: 이응준, David Barr, Jakub Narebski, Yi, EungJun, Jiang Xin.
Thread: https://gitlist.dev/t/29979

## 이응준, 2012-03-17 13:15

Subject: How to reorder all commits include the initial commit
Message-ID: <CAFT+Tg_DmqxiK1qw1YvNZtY07ZaZ89+JWohdWw2fm8f=6hAwiQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAFT%2BTg_DmqxiK1qw1YvNZtY07ZaZ89%2BJWohdWw2fm8f%3D6hAwiQ%40mail.gmail.com

```
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, 2012-03-17 14:06

Subject: Re: How to reorder all commits include the initial commit
Message-ID: <CAFfmPPOhjDhdry2yqgSyAVhaPF-fhXOPOAWRahFkFFssQmz89A@mail.gmail.com>
URL: https://gitlist.dev/e/CAFfmPPOhjDhdry2yqgSyAVhaPF-fhXOPOAWRahFkFFssQmz89A%40mail.gmail.com
In-Reply-To: <CAFT+Tg_DmqxiK1qw1YvNZtY07ZaZ89+JWohdWw2fm8f=6hAwiQ@mail.gmail.com>

```
On Sun, Mar 18, 2012 at 12:15 AM, 이응준 <semtlenori@gmail.com> wrote:
> 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, 2012-03-17 14:26

Subject: Re: How to reorder all commits include the initial commit
Message-ID: <m3k42jl2wn.fsf@localhost.localdomain>
URL: https://gitlist.dev/e/m3k42jl2wn.fsf%40localhost.localdomain
In-Reply-To: <CAFT+Tg_DmqxiK1qw1YvNZtY07ZaZ89+JWohdWw2fm8f=6hAwiQ@mail.gmail.com>

```
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

```

## Yi, EungJun, 2012-03-17 19:45

Subject: Re: How to reorder all commits include the initial commit
Message-ID: <CAFT+Tg9UGdBxj5-DTpxDJRVgvBCnmkmgm6nzVek0KeELgsNneQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAFT%2BTg9UGdBxj5-DTpxDJRVgvBCnmkmgm6nzVek0KeELgsNneQ%40mail.gmail.com
In-Reply-To: <m3k42jl2wn.fsf@localhost.localdomain>

```
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>님의 말:
> 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, 2012-03-18 02:58

Subject: Re: How to reorder all commits include the initial commit
Message-ID: <CANYiYbGbJxZPOAN9twBPZoD318eA_CLcsVzcFuoHrc32+e96FQ@mail.gmail.com>
URL: https://gitlist.dev/e/CANYiYbGbJxZPOAN9twBPZoD318eA_CLcsVzcFuoHrc32%2Be96FQ%40mail.gmail.com
In-Reply-To: <CAFT+Tg9UGdBxj5-DTpxDJRVgvBCnmkmgm6nzVek0KeELgsNneQ@mail.gmail.com>

```
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, 2012-03-18 19:05

Subject: Re: How to reorder all commits include the initial commit
Message-ID: <CAFT+Tg8=dkFLh_=2pMYH6McZTWr2NjxZHztVG7WcxGMEBmdOwA@mail.gmail.com>
URL: https://gitlist.dev/e/CAFT%2BTg8%3DdkFLh_%3D2pMYH6McZTWr2NjxZHztVG7WcxGMEBmdOwA%40mail.gmail.com
In-Reply-To: <CANYiYbGbJxZPOAN9twBPZoD318eA_CLcsVzcFuoHrc32+e96FQ@mail.gmail.com>

```
2012년 3월 18일 오전 11:58, Jiang Xin <worldhello.net@gmail.com>님의 말:
>> $ 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

```
