Gitignore 文件不忽略上述目录中的文件

Gitignore file not ignoring files from above directory

我正在尝试配置我的 .gitignore 文件,这样我就不会跟踪我的任何日志文件。但是,它无法正常工作,我不确定出了什么问题。

现在,目录结构如下所示:

project_name
    - logs
        - logfile1
        - logfile2
    - src
        - file1.py
        - file2.py
        - .git
        - .gitignore
        - __pycache__/

我的 .gitignore 文件的内容是:

__pycache__/
../logs/
logs/
../logs/log*

它忽略 __pycache__ 就好了,但是 Git 告诉我需要跟踪日志。

Documentation 具体说明(强调我的):

Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file.

您的 .gitignore 文件不在 logs 目录的任何父目录中,因此它无效。