0

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>...]'
Cavern head
  • 21
  • 1
  • 7
  • The commands should work. I personally use `git reset --soft HEAD~1`. If that doesn't work for you, the question is: what is wrong with your `HEAD`? What does `git log -n 1 --pretty=%p` show?. That should print the revision hashes of the ancestor commit(s). If this cannot be obtained, that command should at least produce an error message which might give a hint to the problems' root cause. How does `.git/refs/*` look like? There should be a file in it with the name of your current branch and the content should be the `HEAD`s hash. – Adrian W Mar 02 '22 at 18:33
  • so git `log -n 1 --pretty=%p` show just an empty Iine only did one commit so maybe it's normal, and `.git/refs/*` has a folder **heads** and a folder **tags**, tags is empty and head has a file called master int it (which is the name of my current branch) the file master just has one number in it – Cavern head Mar 02 '22 at 19:05
  • 1
    On Windows CMD, the `^` character needs to be doubled: CMD.EXE uses it as a quote character. So instead of `HEAD^` you need to write `HEAD^^`; CMD.EXE will take away one `^`, passing `HEAD^` to Git. But if you have just the one commit, there is no commit before it, and that's the ultimate problem. – torek Mar 03 '22 at 02:05

2 Answers2

3

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

Adrian W
  • 4,563
  • 11
  • 38
  • 52
0

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 :)

Tigraine
  • 23,358
  • 11
  • 65
  • 110
  • 1
    There are cases where `git revert` should be used instead of rewriting history, but suggesting its use doesn't answer the question being asked. – chepner Mar 02 '22 at 17:54
  • 1
    The error messages suggests that `git checkout HEAD~1` will fail for the same reasons `git reset HEAD~1` did. – chepner Mar 02 '22 at 17:55
  • just to make sure i don't want to delete any files in my computer just want to undo the commit so i can split it in multiple smaller commits, i'm a bit confused on what's going on `git revert HEAD ` makes a new empty commit but `git checkout HEAD~1 git checkout -B main` wouldn't that remove all files and make my computer files empty since there hasn't being any other commit before this one. – Cavern head Mar 02 '22 at 18:34
  • No worries - everything always stays around in your `reflog` so unless you delete the .git folder nothing ever gets lost. But from your error message it looks like you only have 1 commit - but want to undo that commit? I am not 100%.sure I understand why you'd want to delete your primary commit? – Tigraine Mar 02 '22 at 20:45
  • i want to undo the commit because the commit is too big and the azure devops servers don't let me push it. So my goal is like first undo the commit then divide the commit in to many smaller commits. then push one at a time. – Cavern head Mar 02 '22 at 21:12
  • Honestly I think you're doing it wrong if your first commit is too big. But alas - if you just want to commit everything you have in your working folder in smaller increments - just delete the .git folder and do git init afterwards. You can then start with a new empty history and add files one by one into commits etc.. – Tigraine Mar 05 '22 at 14:48