Why git reset HEAD~1 Fails on the First Commit and How to Fix It
If you run git reset HEAD~1 in a repository with only one commit, it stops with fatal: ambiguous argument 'HEAD~1': unknown revision. HEAD~1 means "the parent of the current commit," but the first commit has no parent. This error appears when you try to undo the first commit you made by mistake right after git init. To undo the first commit, use git update-ref -d HEAD. To change only its contents or message, use git commit --amend. Both of these work without an error and exit with code 0.
I tested on Ubuntu 24.04 with Git 2.43.0. I only tested with repositories I created locally. I did not test commits that have already been pushed, remote repositories such as GitHub, or Windows and macOS. I fixed the commit dates to keep the output consistent, so commit hashes will differ when you run these commands yourself.
The full error and what is missing
This repository has README.md and index.html, committed once with the message "First commit". git rev-list --count HEAD reports one commit.
$ git log --oneline
b5fd89b 最初のコミット
$ git rev-list --count HEAD
1
When I ran git reset HEAD~1 to undo this commit, I got the following error. The exit code was 128.
$ git reset HEAD~1; echo "exit=$?"
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
exit=128
The first line means that the argument HEAD~1 is neither a revision (a way of naming a commit) that Git can find nor a file in the working tree. Adding --soft or --hard stopped at the same three lines. After the error, git status --short printed nothing, and the repository was unchanged.
According to the official Git reference for revision syntax, <rev>~ refers to the first parent of that commit, and <rev>~<n> refers to the commit reached by following the first parent n times. HEAD~1 is "the parent of the current commit (HEAD)."
You can check whether a commit has a parent by displaying its contents with git cat-file -p. The newer commit in a repository with two commits has a parent line that contains the hash of the previous commit.
$ git log --oneline
6edbe20 トップページを追加
79fe6f2 最初のコミット
$ git cat-file -p HEAD
tree 90a332b7825d7046172bf102c8a259a173591097
parent 79fe6f2920158a2506dbc71a0eb6248393c42b96
author Neko <neko@example.com> 1794618300 +0900
committer Neko <neko@example.com> 1794618300 +0900
トップページを追加
In a repository with only one commit, the parent line is missing. There is nothing to follow, so HEAD~1 cannot point to anything.
$ git cat-file -p HEAD
tree 90a332b7825d7046172bf102c8a259a173591097
author Neko <neko@example.com> 1794618000 +0900
committer Neko <neko@example.com> 1794618000 +0900
最初のコミット

In a repository with two commits, the same git reset HEAD~1 succeeds, and HEAD moves back to the first commit. The index.html added in the second commit remains in the folder as a file.
$ git reset HEAD~1; echo "exit=$?"
exit=0
$ git log --oneline
79fe6f2 最初のコミット
$ git status --short
?? index.html
You can ignore the "Use '–' to separate…" hint
The second and third lines are a general hint that says to use -- if you want to separate revisions from file names. Reading setup.c in the Git 2.43.0 source, this message is the last-resort output for an argument that cannot be interpreted as either a revision or a file name.
I also tried adding -- as the hint suggests, as in git reset HEAD~1 --. The message changed, but the command still failed.
$ git reset HEAD~1 --; echo "exit=$?"
fatal: Failed to resolve 'HEAD~1' as a valid revision.
exit=128
This message says directly that HEAD~1 cannot be resolved as a revision. Adding -- does not fix it, because the problem is not that Git mistook the argument for a file name. The problem is that no commit exists for HEAD~1 to point to.
Undo the first commit: git update-ref -d HEAD
To make the first commit disappear, delete the branch reference (ref) that points to it. According to the official reference for git update-ref, -d deletes the specified ref. Passing HEAD deletes the current branch that HEAD points to (here, main), which returns the repository to a state with no commits.
$ git update-ref -d HEAD; echo "exit=$?"
exit=0
$ git log --oneline; echo "exit=$?"
fatal: your current branch 'main' does not have any commits yet
exit=128
$ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
new file: index.html
git status shows No commits yet, which matches the state right after git init and git add. The two files remain staged, so the result is the same as what git reset --soft HEAD~1 would have produced. After I fixed the contents and committed again, I got a new first commit.
$ git commit -q -m "最初のコミット(やり直し)"; echo "exit=$?"
exit=0
$ git log --oneline
982d986 最初のコミット(やり直し)
git update-ref -d HEAD deletes only the branch. The commit b5fd89b was still in the repository. I checked its object type with git cat-file -t, and it reported commit.
$ git cat-file -t b5fd89b121fa6eb4fe71e320ce2bb8d8b1c351ed; echo "exit=$?"
commit
exit=0
If you also want to unstage the files
If you want to unstage the files as well, as git reset HEAD~1 (--mixed) does, run git reset with no arguments afterward. The files become untracked (??), and they remain in the folder.
$ git update-ref -d HEAD; echo "exit=$?"
exit=0
$ git reset; echo "exit=$?"
exit=0
$ git status --short
?? README.md
?? index.html
If you write git reset HEAD with HEAD explicitly, you get the same kind of error, because the branch HEAD points to (main) has no commits yet.
$ git reset HEAD; echo "exit=$?"
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
exit=128
According to the official reference for git reset, the commit used when no argument is given is HEAD. The results still differ. In builtin/reset.c in the Git 2.43.0 source, when no argument is given and no commit exists yet, Git resets to the empty state. The HEAD argument, on the other hand, fails earlier, because it cannot be interpreted as a revision.
Use git update-ref -d HEAD only when the branch has exactly one commit. If you run it on a branch with two or more commits, the whole branch is deleted, and git log reports does not have any commits yet. git reflog also failed with the same error, so I could not look up the hash from before the deletion. In my test, I was able to restore the commit by passing a hash I had recorded beforehand to git reset --soft <hash>. Before you run the command, confirm that git rev-list --count HEAD returns 1, and note the hash shown by git log --oneline.
To change only the contents or message, use git commit –amend
If your goal is to add a file you forgot or to fix the message, it is easier to rewrite the commit than to undo it. According to the official reference for git commit, --amend replaces the tip commit of the current branch with a new commit. The new commit has the same parent as the original. The first commit has no parent, so the rewritten commit also has no parent.
I ran git add on the forgotten style.css and then committed with --amend. There was no error. The commit count stayed at one, and the contents and message were replaced.
$ git commit -q --amend -m "最初のコミット(style.css を追加)"; echo "exit=$?"
exit=0
$ git log --oneline
af01d5d 最初のコミット(style.css を追加)
$ git rev-list --count HEAD
1
$ git show --stat --format=%s HEAD
最初のコミット(style.css を追加)
README.md | 1 +
index.html | 1 +
style.css | 1 +
3 files changed, 3 insertions(+)
I cover how to use --amend in more detail in a separate article, "How to Fix the Previous Commit with git commit –amend." The differences between --soft, --mixed, and --hard for undoing the second and later commits are summarized in another article, "Three Ways to Undo the Last Commit with git reset and How They Differ."





