red888
red888

Reputation: 31520

fatal: Needed a single revision

My repo has 3 commits. I want to squash to one.

I ran git rebase -i HEAD~3 and got this error:

fatal: Needed a single revision
invalid upstream HEAD~3

I was able to run git rebase -i HEAD~2 and then git push origin +master and now I have 2 commits.

But why couldn't I do git rebase -i HEAD~3?

Upvotes: 3

Views: 12212

Answers (2)

VonC
VonC

Reputation: 1323223

With Git 2.30.1 (Q1 2021), a "git rebase"(man) will give a more explicit error message (since HEAD~3 does not exist in your case):

fatal: no such branch/commit 'HEAD~3'

See commit ca5120c (01 Jan 2021) by René Scharfe (rscharfe).
(Merged by Junio C Hamano -- gitster -- in commit df26861, 15 Jan 2021)

rebase: verify commit parameter

Reported-by: LeSeulArtichaut
Signed-off-by: René Scharfe

If the user specifies a base commit to switch to, check if it actually references a commit right away to avoid getting confused later on when it turns out to be an invalid object.

Upvotes: 0

axiac
axiac

Reputation: 72177

HEAD~3 is the grand-grand-parent of the current commit. But since there are only 3 commits, there is no grand-grand-parent (the first commit is the grand-parent of the current commit).

You can achieve the desired outcome by using git reset followed by git commit:

git reset --soft HEAD~2
git commit --amend

git reset --soft moves the HEAD to the provided commit but doesn't change the work tree and the index. The HEAD now points to the first commit but the working tree and the index (the staged files) are synchronized with the original HEAD; all the changes between the first commit and the third commit are now staged, waiting to be committed.

git commit --amend updates the current commit (it is the first commit after you run git reset --soft HEAD~2).

Upvotes: 2

Related Questions