Git status shows file as deleted on local even though it was not deleted

Viewed 384

I see a deleted local file on my working tree and it is driving me crazy. Here is what is happening:

I clone the remote repo to my local machine:

git clone <my repo>

I checkout master and view its status

git checkout master
git status

And even though I have not done anything with it, I see this file deleted from my local machine

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    xyz.PNG

no changes added to commit (use "git add" and/or "git commit -a")

I can see this file on the remote repo. And sure enough, if I do:

git reset --hard HEAD

or

git checkout .

The file appears back and the status is clean again.

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

But if I checkout some other branch, and then switch back to master again, the delete message turns up again. It is almost as if there is something that is deleting this one specific file from my local every time I switch to master, but I have no idea what that would be.

I have looked at Unstaged changes show file as deleted even though it exists and there does not seem to be a space in the filename (no quotes around file name):

git status --short
D xyz.PNG 
1 Answers

This:

(and the file actually does disappear from the tree until I do a git reset)

is the most important item. It means that Git isn't lying when it says that the file is deleted, but the deletion is not staged for commit. The D xyz.PNG (note that there's a leading space before the D here) as a short status means the same thing: the file has mysteriously gone missing from your working tree. (Note that if you had removed it yourself, this would not be mysterious: it would be expected.)

The second most important thing is that your working tree is just a plain old set of directories-and-files (folders-and-files, if you prefer the "folder" term) on your computer. This means anything else on your computer can engage in a drive-by shooting and take out the files entirely, and presumably this is what is happening. If git checkout had failed to check out the file initially, Git should have noticed this and complained. Without any complaint, that implies that git checkout was successful and placing the ordinary file into the ordinary directory on your computer. Something else must have removed it.

Your mission, should you choose to accept it, is to find out what this "something" is. It's not Git. (Is it some sort of antivirus software? If so, why does a later reset or checkout not get the same treatment? Are you storing your working tree in a shared or cloud-synced location?)

Related