Search code examples
gitgitignoregit-subtree

How do I "reverse" a gitignore file pattern in a subtree subdirectory?


I have a repo that contains a subtree that has some build and config files.

main
├── .gitignore
└── build (subtree)
    ├── .gitignore
    └── config.user.json
    └── config.site.json
└── index.js

I would like to ignore the config.user.json from both the main repo and build subtree, but keep config.site.json in the main repo, but ignore it in the build subtree.

main/.gitignore

!build/config.site.json # (this is supposed to reverse the ignore)
build/config.user.json

build/.gitignore

build/config.site.json
build/config.user.json

Is this possible, or should I put my config files in a different folder that the build. I have a feeling that the .gitignore in the subtree will override the .gitignore in the main repo and not the other way around.

REF: Git Subtree Specific Ignoring

Cheers


Solution

  • It's not going to work that way. The Git documentation on gitignore says (emphasis mine)

    Each line in a gitignore file specifies a pattern. When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):

    • Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file.

    https://git-scm.com/docs/gitignore