Windows 10/Insiders
Repro:

Looking for git in: C:\Program Files\Git\cmd\git.exe
Using git 2.13.2.windows.1 from C:\Program Files\Git\cmd\git.exe
> git rev-parse --show-toplevel
fatal: Not a git repository (or any of the parent directories): .git
> git rev-parse --show-toplevel
fatal: Not a git repository (or any of the parent directories): .git
> git rev-parse --show-toplevel
> git init
> git rev-parse --show-toplevel
> git config --get commit.template
Open repository: c:\Users\daimms.REDMOND\Documents\playground\t
> git status -z -u
> git symbolic-ref --short HEAD
> git rev-parse master
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose
> git check-ignore -z --stdin
> git check-ignore -z --stdin
> git add -A -- c:\Users\daimms.REDMOND\Documents\playground\t\script-1.bat
> git status -z -u
> git symbolic-ref --short HEAD
> git rev-parse master
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose
> git add -A -- c:\Users\daimms.REDMOND\Documents\playground\t\.vscode\tasks.json
> git status -z -u
> git symbolic-ref --short HEAD
> git rev-parse master
fatal: ambiguous argument 'master': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose
> git commit --quiet --allow-empty-message --file -
> git status -z -u
> git symbolic-ref --short HEAD
> git rev-parse master
> git rev-parse --symbolic-full-name --abbrev-ref master@{u}
fatal: no upstream configured for branch 'master'
> git for-each-ref --format %(refname) %(objectname) --sort -committerdate
> git remote --verbose
> git config --get commit.template
> git show -s --format=%H
%B HEAD
> git reset HEAD~
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>...]'
> git show -s --format=%H
%B HEAD
> git reset HEAD~
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>...]'
@Tyriar and @joaomoreno I would like to work on this issue.
@sergiorojasa 馃憤
@Tyriar I tried to fix the issue but I couldn't fix it.
@sergiorojasa Do you mind if I work on this?
We can reproduce this issue on macOS as well. Also we can get the same error message on terminal if we execute git reset HEAD~ because Code is doing the same thing through CommandCenter#undoCommit (in extensions/git/src/commands.ts) 鉃★笍 Repository#reset (in extensions/git/src/git.ts)
$ git reset HEAD~
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>...]'
The error occurs because if the last commit is the initial commit (or no parents) of the repository, there is no HEAD~.
To undo the last commit, we can do the following depending on the last commit.
git reset HEAD~git update-ref -d HEAD followed by git rm -fr .git rm --cached -r . cf. How to revert initial git commit?To check whther the last commit is the initial commit, we can run 鉃★笍 extend git rev-list with --parents option.
If it gives the current commit followed by other commits (i.e. parents), we know the current commit is not the initial commit.Repository#getCommit (in extensions/git/src/git.ts) to also retrieve previous commit hashes (cf. 5c139f7fe8b74cb553be422e3ac668607d9d8060).
$ git rev-list --all --parents --max-count=1
43e1f4b00f3cdb5e8f280ce6ea29871e849cf047 53610b8ef24cb8ceba35a0c244cf841324ad1276
Any thoughts on 猬嗭笍, @joaomoreno?
@ryu1kn I am not sure that your listed desirable outcome matches the solution proposed.
Desired Outcome:
- When the last commit is the initial commit
- Bring the repository back to clean (no uncommitted/staged files), no commit state
However, your proposed solution suggests doing git rm -fr . which would delete the files added to the initial commit. However, if you want to just unstage all files for commit, maybe git rm --cached -r -- . is the correct option?
Thanks @andrewtomai for checking my fix suggestion. I somehow misunderstood that "Undo Last Commit" not just reset the commit but Bring the repository back to the clean (no uncommitted/staged files), previous commit state. So the desirable outcome section is wrong, so is the solution (i.e. git rm -fr .)
Updated my fix suggestion comment
Actually, I can see if a commit has parents or not by extending Repository#getCommit (in extensions/git/src/git.ts) like this 5c139f7fe8b74cb553be422e3ac668607d9d8060. Updated the fix suggestion again.
Oh wow, sorry I didn't notice that rebasing on my branch posted so many refs on this issue...
The way to achieve this might be much simpler. Simply delete the branch and recreate it. Would that work?
Hmm... do you mean something like this?
# We want to reset the only commit on "master" branch
$ git checkout -b temporary # we need to pick a branch name which doesn't conflict with others
$ git branch -D master
$ git checkout --orphan master
$ git rm --cached -r .
$ git branch -D temporary
I think the current way 猬囷笍is simpler.
# We want to reset the only commit on "master" branch
$ git update-ref -d HEAD
$ git rm --cached -r .
If you know the better way, I'm happy to revise the PR. Can you list the git commands like above in that case? 馃槂
You're right! 馃憤
@ryu1kn I tried your way. Works for me. Thanks!
Thanks @paulsabandal , I've created a PR, #47578, and am wondering if someone in the code team can have a look at it...
@ryu1kn, I tried on shell your
git update-ref -d HEAD
git rm --cached -r .
but it returns me an
error: the following file has staged content different from both the
file and the HEAD:
(use -f to force removal)
I managed to get what I wanted with force.
I know nothing of vscode, just passing from here :) , but I thought it could interest someone (maybe to specify it in unstageAll, if not implicit already).
Most helpful comment
Cause of the issue
We can reproduce this issue on macOS as well. Also we can get the same error message on terminal if we execute
git reset HEAD~because Code is doing the same thing throughCommandCenter#undoCommit(inextensions/git/src/commands.ts) 鉃★笍Repository#reset(inextensions/git/src/git.ts)The error occurs because if the last commit is the initial commit (or no parents) of the repository, there is no
HEAD~.Desirable outcome of "Undo Last Commit" (I think)
Bring the repository back to the clean (no uncommitted/staged files), previous commit state鉃★笍Keep the last commit changes as unstaged changesBring the repository back to clean (no uncommitted/staged files), no commit state鉃★笍Leave the initial commit changes as unstaged changes, but no commit stateSolution to the issue (my suggestion)
To undo the last commit, we can do the following depending on the last commit.
git reset HEAD~git update-ref -d HEADfollowed bygit rm -fr .git rm --cached -r .cf. How to revert initial git commit?To check whther the last commit is the initial commit,
we can run鉃★笍 extendgit rev-listwith--parentsoption.If it gives the current commit followed by other commits (i.e. parents), we know the current commit is not the initial commit.
Repository#getCommit(inextensions/git/src/git.ts) to also retrieve previous commit hashes (cf. 5c139f7fe8b74cb553be422e3ac668607d9d8060).