2

I first found that .gitignore was not working for me even if I've specified the files that I didn't want to add. I tried using git rm --cached to untrack those files but I was stuck with the following error:

fatal: pathspec 'System2/Benchmark/sys2.xtc' did not match any files

I've made sure that the file did exist, but git seemed not able to find it.

Since the size of this file is larger than 100 MB, I really needed to make .gitignore work for it. I'm wondering how I could solve the error I had when running git rm --cached. Thanks!

Jack
  • 81
  • 3
  • 11
  • The file may well exist in some existing commits, but not in the index. If that's the case, `git rm --cached` cannot remove it from the index, as it's not in the index. – torek Feb 04 '22 at 07:27
  • @torek Thanks for the reply! So if that is the case, how can I remove the file from the repository such that .gitignore would ignore the file when I run git add? – Jack Feb 04 '22 at 07:32

1 Answers1

1

, how can I remove the file from the repository such that .gitignore would ignore the file when I run git add

First, you don't have to git add to check if the .gitignore is working or not.
A simple git status is enough.
Or git check-ignore -v -- System2/Benchmark/sys2.xtc (assuming the file is there): a rule would mean the file is ignored. Empty output means the file is not ignored.

Second, make sure your file is not in a submodule or a nested Git repository, as any git rm command done from the parent repository would not apply.

Third, if the file was committed in the past (and since deleted), you might need to cleanup your history with git filter-repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for your response. I've used `git check-ignore -v -- System2/Benchmark/sys2.xtc` and as expected, the file was not ignored. My repository is not in a submodule nor is nested so I'm wondering why `git rm --cached` would fail. I don't remember if the file was committed or not, but I assume that if it was once committed, I've used `git reset HEAD^ --soft` because the file is too large to be pushed. I think I still get to get `git rm` working, or .gitignore would not ignore the file anyway. – Jack Feb 04 '22 at 08:13
  • @Wei-TseWEHS7661 Does the sys2.txt file exist on your filesystem in `System2/Benchmark` (with the same upper/lowercase)? – VonC Feb 04 '22 at 08:14
  • Yes, the file does exist. – Jack Feb 04 '22 at 09:03
  • @Wei-TseWEHS7661 If the file exists, but git rm fails, that means it is not versioned yet. Can you therefore try more generic ignore rules, like ignoring the all folder, just to test (witch `git check-ignore -v`) that the existing file is ignored? For instance, a rule like `System2/Benchmark`, assuming the `.gitignore` is in the parent folder of `System2` – VonC Feb 04 '22 at 09:05
  • Hi, I've tried adding the folder to `.gitignore` (which is in the parent folder of `System2`) but the folder was not ignored according to the empty result returned by `git check-ignore -v`. – Jack Feb 04 '22 at 09:15
  • @Wei-TseWEHS7661 Warning, `git check-ignore -v --` only work for a file inside the ignored folder, not for the folder itself (which is not tracked per se: only its content is tracked) – VonC Feb 04 '22 at 09:19