Can a Git repository contain multiple .gitignore files in the root folder?

No, a Git repository should only have one .gitignore file in the root folder. The .gitignore file in the root directory of your repository is meant to apply its rules to the entire repository. However, you can have multiple .gitignore files within different subdirectories of your repository, each applying only to the directory in which it is located and its subdirectories. This allows you to have specific ignore rules for different parts of your project.

For example:

  • / (root directory)
    • .gitignore
    • /src
      • .gitignore
    • /docs
      • .gitignore

Each .gitignore file can contain rules relevant to the files and directories at its level and below.