I want to make git ignore all files except *.c, *.cpp and *.py.
I tried this:
*
!*.c
!*.cpp
!*.py
but this didn't work; it ignored even the *.c, *cpp and *.py files.
git status result before adding this .gitignore file:
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: 2065/b.cpp
new file: 2065/sample.c
new file: 2065/sample.py
Untracked files:
(use "git add <file>..." to include in what will be committed)
.vscode/
2067/a
2067/a.input
2067/case-permutations.cpp
2067/cases
2069/custom.input
git status result after adding this .gitignore file:
On branch main
nothing to commit, working tree clean
NOTE: before checking the git status result after adding .gitignore file, I used git restore to unstage those three files that were staged before so that you can see that this ignore file is not showing the c, cpp, and py files. otherwise, you could see those staged files even after adding the ignore file because they were already staged, so git can see them even after ignore file blocked them.
*
ignores everything including directories. As it's said in the docs: "It is not possible to re-include a file if a parent directory of that file is excluded."
In your case the directory 2065
is ignored so Git doesn't even look into it for unignored files. To fix the problem unignore all directories:
*
!*/
!*.c
!*.cpp
!*.py