1

I have a project directory with multiple subdirectories that contain various files with distinct file extensions. Let's say I only want to track Python source files (*.py) and C++ source files (*.cpp).

I tried this:

# ignore all files ...
*.*

# ... except these
!.gitignore     # .gitignore file
!*.py           # python source files
!*.cpp          # c++ source files

This keeps track of the correct files that are already in the repository; however, the problem is that VSCode won't automatically track new files that I add to the project, even if they are *.py or *.cpp files.

Carlos
  • 586
  • 1
  • 9
  • 19

2 Answers2

1

My problem is that I was actually using inline comments, which now I realize are not allowed. This now works:

# ignore all files ...
*.*

# ... except these:

# .gitignore file
!.gitignore

# python source files
!*.py

# c++ source files
!*.cpp

Carlos
  • 586
  • 1
  • 9
  • 19
0

Using exception rules ("ignore all except xx") means following the rule

It is not possible to re-include a file if a parent directory of that file is excluded.

You can check if your new *.py files are still ignored with git check-ignore -v -- /path/to/new/file.py.

Add a whitelist rules for folders

# ignore all files ...
*.*

# whitelist folder
!*/

# ... except these files
!.gitignore     # .gitignore file
!*.py           # python source files
!*.cpp          # c++ source files
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I tried adding `!*/` as you show; I also tried adding `!*/` + `!*/path/to/folder/containing/file/` but is still ignored. Running `git check-ignore` returns: `.gitignore:2:*.* ./path/to/folder/containing/file/new_file.py`. – Carlos Jan 19 '23 at 12:53
  • Ok I found the problem: I actually had inline comments, which now I realise are not allowed. – Carlos Jan 19 '23 at 13:09
  • @Carlos That should work indeed, considering the `*.*` first rule might not ignore folders themselves. – VonC Jan 19 '23 at 20:01