Search code examples
gitgitignoresubdirectory

A simple way to apply a .gitignore file to all subfolders containing individual projects in the repository


I have a Git repository containing a lot of subfolders, each of which is an IntelliJ IDEA project.

I have found a JetBrains .gitignore which ignores files in an IntelliJ IDEA project and I want to apply it to all such projects in these subfolders.

Of course, I can make a copy for each project, or leave it in the root folder and edit the patterns in the .gitignore file to match files in subfolders.

However, I am wondering whether there is a simpler way to do this rather than making copies or editing a long .gitignore file.


Solution

  • To ignore same set of files put that .gitignore file into root of your Git repository and prefix all lines that apply to all subdirectories with wildcard /*/. Same set of files will then be ignored in all subdirectories, with no need to update .gitignore when you add or remove subdirectory.

    If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself.

    So if you have following directory structure:

    my_repo/
      .gitignore
      project1/
        foo.txt
        bar.txt
      project2/
        foo.txt
        bar.txt
    

    And want to ignore bar.txt in all project directories then your .gitignore will be:

    /*/bar.txt