Search code examples
gitgitignore

How do I ignore numeric logs in git?


Python RotatingFileHandler creates logs like foo.log, foo.log.1, ..., foo.log.213....

How do I ignore all these in git? (I know that *.log are ignored automatically).

E.g., adding

*.log.[0-9]
*.log.[0-9][0-9]
*.log.[0-9][0-9][0-9]

to .gitignore would probably work for the first 1,000 log files, it looks ugly.

Is there a better way?


Solution

  • If you want to be really precise and ignore only files with numeric suffix after .log. and excluding things like x.log.1bar, then this will do it:

    *.log.*
    !*.log.*[^0-9]*
    

    This will match everything containing .log., excluding patterns where the suffix after .log. contains non-digits. This also excludes *.log.. In effect, it matches only files with numeric suffix after .log..