1

I cannot get .gitignore to work with Visual Studio 2019 Community.

enter image description here

Above is an image of the "Team Explorer" tab showing a portion of the list of files which are to be committed. The directory in which these files appear, x64\Debug, is under C:\Users\username\Source\repos\Tetris_System\Tetris_Game. C:\Users\username\Source\repos\Tetris_System contains the .git and .vs folders and the .gitignore file.

I have a very simple .gitignore file with a single line in it:

*.obj

My understanding is, that this single line should remove all the *.obj files from the commit list. Shouldn't those *.obj files disappear from the list?

From previous commits, all those *.obj are on the GitHub repository.

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
user34299
  • 377
  • 2
  • 11
  • 1
    No. `.gitignore` is just for file that are not tracked (if you think about it twice, it makes sense because you don't want that a tracked file stopped to be committed because you make a mistake with a rule). You have to untrack them before it takes effect. – Philippe Jul 25 '20 at 18:12

1 Answers1

9

The .gitignore-file only works on untracked files.

Git sees every file in your working copy as one of three things:

  1. tracked - a file which has been previously staged or committed
  2. untracked - a file which has not been staged or committed
  3. ignored - a file which Git has been explicitly told to ignore

If you have tracked files, the .gitignore-file doesn't works.

To untrack a single file, stop tracking the file but not delete it from the system use:

git rm --cached filename

To untrack every file in .gitignore (first commit any outstanding code changes, and then run):

git rm -r --cached

This removes any changed files from the index (staging area), then run:

git add .

and Commit it:

git commit -m ".gitignore file is now working"
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
  • 1
    When I did `git rm -r --cached` I got `fatal: No pathspec was given. Which files should I remove?`, so added a pathspec, with `git rm -r --cached .` Then I got a very long output of files being `rm`d. I entered your next two commands, and it seems to have worked. – Stephen Hosking Sep 22 '22 at 07:13