Reputation: 152
I have a tracked file, "file.txt". I decided to physically delete it using Windows file explorer. Then I do a "git add file.txt". Should this not trigger "fatal: pathspec 'file.txt' did not match any files" since the file no longer exists?
Upvotes: 0
Views: 521
Reputation: 78833
No. If you have a file in the index, and do not have the file in the working directory, then git add
will stage the deletion. (In other words, it behaves like git rm
).
$ rm exists
$ git ls-files --stage exists
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0 exists
$ git add exists
$ git ls-files --stage exists
$ ls -Flas exists
ls: exists: No such file or directory
However, if the file does not exist in either the index or the working directory, then git will error:
$ ls -Flas nonexistent
ls: nonexistent: No such file or directory
$ git ls-files --stage nonexistent
$ git add nonexistent
fatal: pathspec 'nonexistent' did not match any files
Upvotes: 2
Reputation: 125037
Should this not trigger "fatal: pathspec 'file.txt' did not match any files" since the file no longer exists?
No, it should not. file.txt
is still a file that git is tracking, even if it doesn't exist in the local file system. It did exist, and then you deleted it. If you had run git status
at that point, you'd see file.txt
marked as deleted but not staged. When you run git add file.txt
, and then git status
, you'll see that file.txt
is marked as deleted and staged. The next git commit
will then add that change to the repository, so that the file won't show up in the repo beyond that commit.
Upvotes: 1