131

I initialized a new git in my project and I have only two commits so far. My log is like below

git log
commit e515e5b8dcbd8f1ea4a7a7d4a1efb82a1a0aee7a
Author: Olkun Mustafa <olkun.mustafa@gmail.com>
Date:   Fri Oct 3 10:04:20 2014 +0300

    Temp commit

commit 71781bf0a7807351a56d5155dac94169ea700527
Author: Olkun Mustafa <olkun.mustafa@gmail.com>
Date:   Fri Oct 3 10:01:42 2014 +0300

    First Commit

When I try to rebase this commits I get error like below

git rebase --interactive HEAD~2
fatal: Needed a single revision
invalid upstream HEAD~2

I quite research at google but I haven't found solution till now.

Olkunmustafa
  • 3,103
  • 9
  • 42
  • 55

2 Answers2

254

In your case, there is no HEAD~2, since you only have 2 commits, hence the "Needed a single revision" error message.
Try:

 git rebase -i --root

see more about --root at "Change first commit of project with Git?"

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    I was getting only the "fatal: invalid upstream head~2" even though I had four commits in the log. The "single revision" message wasn't showing for me. However, this fixed me right up! Thanks! – Austen Hoogen Mar 01 '20 at 23:20
16

This doesn't apply to your case, but may help others. If on Linux, make sure HEAD is capitalized. If you use lowercase head like the first example below (because you are used to working on Windows or Mac and those allow lowercase head), you will get the fatal: Needed a single revision error!

Or you can use @ as an alias for HEAD, then you won't need to worry to forgetting to capitalize it.

# wrong on linux
git rebase --interactive head~2

# correct on linux
git rebase --interactive HEAD~2

# correct on all
git rebase --interactive @~2
wisbucky
  • 33,218
  • 10
  • 150
  • 101