Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

git restore –staged vs git rm –cached

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading