Search code examples
gitgitignore

Git : Ignore all file and directory but not some codes in subdirectory


I create a git repository named test_dir/ with git init,

in test_dir, I have some files and a subdirectory named code as below.

$ ls test_dir/
a.dat  b.dat  code/  c.py

$ ls test_dir/code/
e.dat  f.dat  test2.py  test3.py  test.py

I want to create a .gitignore file to ignore all files but not the python script file (*.py) in git root directory and in its subdirectory.

How should I configure my .gitignore?


Solution

  • You can use something like

    *
    !.gitignore
    !*/
    !**/*.py