Git : 忽略所有文件和目录,但不包括子目录中的某些代码

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

我使用 git init 创建了一个名为 test_dir/ 的 git 存储库,

test_dir中,我有一些文件和一个名为代码的子目录如下。

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

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

我想创建一个 .gitignore 文件来忽略 git 根目录及其子目录中的所有文件,但不包括 python 脚本文件 (*.py)。

我应该如何配置我的 .gitignore

使用!避免忽略。对于子目录 !*/

你可以写两条规则,一条忽略,一条排除。它应该是这样的:

test_dir/code/*
!test_dir/code/*.py

您可以使用

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