Home > OS >  How to .gitignore all files except old and new files with a given file extension
How to .gitignore all files except old and new files with a given file extension

Time:01-20

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.

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • git
  • Related