-2

I am trying to remove a file from the git repository. However, when I do:

git rm <file name>
fatal: pathspec 'file1.txt' did not match any files

fatal: pathspec 'file1.txt' did not match any files

The two files still untracked files!

grg
  • 5,023
  • 3
  • 34
  • 50
Yasser
  • 23
  • 3
  • 1
    remove the files (using simply rm) and commit the 'change' (=deletions) to the repo. – StarShine Jan 19 '21 at 22:11
  • 2
    Untracked files aren't in the current commit and won't be in the next commit. *None* of the files that you can see right now are *in* the repository; the ones that are in the repository got copied out to make (some of) the ones you can see. The files you can see and work on/with are *your* files, not Git's; Git copies committed files *to* this area on checkout, but doesn't remove other files that you created yourself. – torek Jan 19 '21 at 22:31
  • Does this answer your question? [How can I delete a file from a Git repository?](https://stackoverflow.com/questions/2047465/how-can-i-delete-a-file-from-a-git-repository) – krismath Jan 20 '21 at 05:14
  • @torek I know that's shorter than your usual length of answer, but I think that does make a good answer here? It's basically all I'd answer with – grg Feb 09 '21 at 19:31

1 Answers1

0

Here, git rm is telling you that the file you are telling it to remove is already not going to be in the next commit you make:

$ git rm new.html
fatal: pathspec 'new.html' did not match any files

You added this last line:

The two files still untracked files!

I don't quite know what you mean to say here (among other things, this sentence has no verb), but the fact is that an untracked file is a file that won't be in the next commit you make. It's therefore impossible for Git to remove it from the proposed next commit: it's not in the proposed next commit.

The git rm command will remove a file from two places: the proposed next commit, and your working tree. For this to happen, the file has to be in both places.

Files that you can see—that show up in ls output—are not actually the files that are in the repository. When you run git checkout, Git will copy files that are in the repository into your working tree, but other files that already exist in your working tree—that you put there yourself, for instance—just sit there, unmodified. They're not in the proposed next commit, because the initial proposed next commit consists of the files copied out of the current commit.

When git status reports files as untracked, that means they are not in your proposed next commit, so that if you commit now, they will not be in that commit.

torek
  • 448,244
  • 59
  • 642
  • 775