Home > OS >  How can undo my last commit? git reset --soft HEAD^ nor git reset --soft HEAD~1 are working?
How can undo my last commit? git reset --soft HEAD^ nor git reset --soft HEAD~1 are working?

Time:03-03

This question seems to have being asked multiple times but non of the solutions I have seen are working for me.

I'm using git on windows command prompt but I also tried git bash they both produce nearly exactly the same results.

git reset --soft HEAD^
git reset --soft "HEAD^"
git reset --soft HEAD~1
git reset --soft "HEAD~1"

for each of these commands I always get:

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>...]'

except with "git reset --soft HEAD^" but it's a similar feedback

More? 1
fatal: ambiguous argument 'HEAD1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

CodePudding user response:

Your repository is on the initial commit. This commit does not have an ancestor. Therefore, any commands which access an ancestor of HEAD fail.

That case is handled in this question. I would suggest applying that question's accepted answer, which is to delete the current branch altogether (since it contains the commit which you want to delete as the only commit) with:

git update-ref -d HEAD

Your files remain intact and you can start over with comitting. If you had already pushed your previous commit, please consider the advice in this answer

CodePudding user response:

git revert HEAD will simply record a new commit that undoes your last commit.

You can also go ahead and just check out your previous commit and set the branch to that commit instead of the last one (remember a branch is just a pointer). That essentially makes your last commit go away.

Assuming you are on main

git checkout HEAD~1
git checkout -B main

Now your commit is gone :)

  • Related