0

I'm trying to configure my .gitignore file so that I don't track any of my log files. However, it's not working properly and I'm not sure what's wrong.

Right now, the directory structure looks like this:

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

The contents of my .gitignore file are:

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

It's ignoring __pycache__ just fine, but Git is telling me that logs needs to be tracked.

Sean
  • 2,890
  • 8
  • 36
  • 78
  • 3
    Maybe you could create another .gitignore in the above directory? – Marsman Aug 06 '20 at 02:25
  • That actually did the trick, thanks. Is it impossible to handle files in other directories with one .gitignore? – Sean Aug 06 '20 at 02:26
  • 1
    Why should that ignore be able to control files in other directories that is not a descendant? – Jeff Mercado Aug 06 '20 at 02:28
  • 2
    If you want to handle files with one .gitignore, I think you need to put the .gitignore file in the top directory of your git repo. – Marsman Aug 06 '20 at 02:30
  • @JeffMercado I'm not saying it should, I was just curious as to whether it's possible or not. – Sean Aug 06 '20 at 02:31
  • 1
    According to https://stackoverflow.com/questions/19488902/ignore-parent-directory-in-git-version-control, I think it's not possible. Maybe the source code of the git would explain why. – Marsman Aug 06 '20 at 02:34
  • 2
    I'm just trying to figure out why you would expect it to. Your directory is like a box and the ignore file describes what is not in the box. You shouldn't expect it to describe what's not in other boxes. – Jeff Mercado Aug 06 '20 at 02:34
  • @JeffMercado Ah that makes sense. I only thought that it may be possible to handle other directories as well, I wasn't aware that the hierarchy should be taken into account. – Sean Aug 06 '20 at 02:36
  • 1
    @Seankala It's not possible what you are asking, see documentation: https://git-scm.com/docs/gitignore – Michał Turczyn Aug 06 '20 at 04:11

1 Answers1

3

Documentation is specific about that (emphasis mine):

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.

Your .gitignore file is not in any of the parent directories of logs directory, so it has no effect.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69