I want to delete my last commit from my project repository. But when i run 'git reset --soft HEAD^ 'or 'git reset --hard HEAD^' ,
it shows 'fatal: ambiguous argument 'HEAD~': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]''
What's the problem?
The problem is simple enough: there is, at this point, only one commit in your repository,1 so HEAD~ or HEAD^—which names the commit before the current commit—has no commit to name. You cannot move the current branch name back one commit as there is no previous commit.
1If you have been using git reset to move the current branch name backwards, there may be more than one commit in your repository, but you've probably made them all "invisible" and very hard to find at this point. The reason for this is that Git finds commits by starting with a branch name and then working backwards to previous commits. The git reset command allows you to move the name so that it selects any arbitrary commit you choose, but once moved, that name now finds that commit and its previous commits.
Pictorially, imagine you have a repository with three commits in it, and just one branch name. Our three commits have random-looking hash IDs and to avoid using them, we'll just call the three commits A, B, and C; we'll have Git use the branch name main to find them:
A <-B <-C <--main (HEAD)
Here, the name main points to (contains the raw hash ID of) the third commit C. Commit C—whatever its real hash ID is—contains a snapshot of all files, plus some metadata, and its metadata contains the hash ID of earlier commit B. So Git can use main to find C, then use C to find B. Commit B, being a commit, also contains a snapshot and metadata, and B's metadata include the hash ID of the first commit A.
If you use git reset to move the name main backwards one hop, e.g., git reset HEAD~, you get this:
C
/
A--B <-- main (HEAD)
Commit C still exists, but without knowing its raw hash ID, you can't find it. The name main helps you and Git find commit B, and commit B points backwards to commit A, so you can find both of these commits, but you can no longer find commit C.
A second git reset results in:
B--C
/
A <-- main (HEAD)
where you and Git can find commit A, but can no longer find B or C, unless you memorized or otherwise saved their hash IDs. You cannot git reset HEAD~ any more, though, because HEAD~ means "the commit before commit A" and there is no such commit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With