Search code examples
gitbashgithubgitignoregit-bash

Ignore files within a subdirectory using gitignore


I'm trying to create a gitignore file for git that ignores any .file within any dir/. I want to ignore all instances of .file inside instances of a dir/. These are specific file formats and directory.

For example, .file is the file format of new.file. The dir/ is the directory that houses new.file. The path is dir/new.file.

I need git to ignore possible instances of .file within possible instances of dir/, which may appear arbitrarily.

I tested these:

**/dir/*.file
*/dir/*.file
*/dir/**/*.file
dir/**/*.file

However, whenever I create a foo/dir/new[*].file, I was able to git add it and also git commit the file.

All new instances of the file format .file within a dir/ directory are added to staging with git add . or git add -A. I expect git to ignore these files.


Solution

  • **/dir/**/*.file seems to work as you specify, at least in my tests. Is is possible that you already have the files in question in the repo and have to remove them first?

    git rm --cached <filepath>