0

I have read about git restore vs git rm --cached, but my question does not ask about the result of the above commands.

I used both Pycharm and Visual Studio Code to coding, and I realize there is a little difference.

On Pycharm, I can use git restore --staged to undo git add but on Visual Studio raise a error fatal: could not resolve HEAD

Example: I created two files and staging them with git add

enter image description here

And then, I want to use git restore --staged to unstaged one of two files, but raise a error.

enter image description here

I try git restore --staged on pycharm, it worked fine

enter image description here

git restore --staged work fine

enter image description here

I don't want to urge about which one to use unstage file, I just want to know why there is a difference on Pycharm and Visual Studio Code.

Thanks

Manh Do
  • 111
  • 8

1 Answers1

1

The two are entirely different commands:

  • git restore is about copying some file(s) from somewhere to somewhere.
  • git rm is about removing some file(s) from somewhere.

Because git restore is about copying a file or files, it needs to know where to get the files. There are three possible sources:

  • the current, or HEAD, commit (which must exist); or
  • any arbitrary commit that you specify (which must exist); or
  • Git's index aka staging area.

Because git rm is about removing a file or files, it only needs to know the name(s) of the file(s) to remove. Adding --cached tells git rm to remove these files only from Git's index aka staging area.

On Pycharm, I can use git restore --staged to undo git add but on Visual Studio [I get the] error fatal: could not resolve HEAD

This error message indicates that you have no commits. That means there's no source from which to restore the file unless you choose the existing staging area as the source. So either there's a bug in VSCode (which is extremely likely1), or you have not yet made your first commit (which is even more likely). Your PyCharm IDE is probably able to do things because you have made your first commit by now.

This is also what your screenshots show.


1VSCode is ridiculously complicated and allows nearly arbitrary plug-in extensions, and judging by what I have seen here on SO, most of the extensions are positively riddled with bugs. That's not exactly VSCode's fault, but it's the kind of thing that would make me think again about using VSCode, if I could stand to use IDEs for any serious work in the first place.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Amazing, Thanks for your detailed explaination. It helps me better understand what i have read about the unstage file. Thank you so much. – Manh Do Oct 24 '22 at 21:41
  • So, I have a question about **current** in **the current, or HEAD, commit (which must exist); or** on your answer. I think it is working directory, what exactly it is, can you describe it more clearly? – Manh Do Oct 24 '22 at 21:44
  • The working directory is not a commit. For (much) more about this, see any of my many more-detailed relatively-introductory Git answers, such as [this one](https://stackoverflow.com/a/71685386/1256452). – torek Oct 24 '22 at 23:32
  • thank you! I need more time to understand it – Manh Do Oct 24 '22 at 23:47