Search code examples
gitdirectorygitignore

How can I specify Git to only ignore files that match a pattern and not a directory?


I ran into a scenario today where there was a naming collision with a file we desire to ignore and a subdirectory in one of our projects.

In a new project that is being set up on Git, there is a directory called src/core/app. Our default Git ignore file excludes any file called core due to it being output if there is a core dump. The .gitignore file contains a similar line, like so:

# Debug files
core

We worked around it by doing a git add -f, but I'm curious if there an available Git ignore syntax that would specify we only want to ignore a file named core and not include any directories that may be named core?


Solution

  • Yes, this is actually possible using negative patterns and a trailing slash (which matches directories). Put these two lines in your .gitignore:

    core
    !core/
    

    This will ignore files named core but not directories named core.