'Can git be made to ignore nested .gitignore files when computing ignores?
I wanted to track some files ignored by a bunch of repos into a single repo.
Here's an example layout:
.
+-- .git
+-- .idea
|-- .gitignore
|-- proj1
| +-- .git
| |-- .gitignore
| |-- foo
| | |-- foo.iml
| |-- bar.c
| |-- proj1.iml
|-- proj2
+-- .git
|-- .gitignore
+-- bar
|-- foo.c
|-- proj2.iml
I want:
- the root-level repo to include all *.iml files, while each sub-repo individually ignores them
- all the repos to be independent (the projects don't depend on the root, and can be manipulated separately)
- this folder structure to be maintained
Any solution?
Solution 1:[1]
The title of the question and the detailed description seem to ask slightly different questions...
With regards to the title of the question: to exclude nested .gitignore files, but not the root .gitignore itself, add the following line to your root .gitignore file:
*/**/.gitignore
(this will only ignore those .gitignore files that are nested at least one folder level deep)
Alternatively, if you only want to ignore .gitignore files in direct child folders, but still want to include all .gitignore files in indirect (i.e., 2nd level, 3rd level,...) sub-folders use:
*/.gitignore
Solution 2:[2]
The trick is: a nested repository whole content is ignored by a parent repository.
In the OP's question, no *.iml file would be tracked by the root-level repository, because of the .git subfolder in proj1 and proj2.
But, assuming that proj1/2 content was actually tracked by the parent repository (git subtree, git subrepo), the .gitignore patterns application rules are clear:
Patterns read from a
.gitignorefile in the same directory as the path, or in any parent directory (up to the top-level of the working tree), with patterns in the higher level files being overridden by those in lower level files down to the directory containing the file.
So:
I only want git to recognize the rules of the root
.gitignore.
That would means a git wrapper script which would:
- delete the
projx/.gitignore - add everything
- restore the deleted
.gitignorefiles
The OP adds in the comments:
Can you setup git to automatically run a "wrapper script"?
Yes: you just define an alias for git (even on Windows, with doskey), which calls your own script.
If I can automatically run a wrappper script on commit that renames the
.gitignoreb4 commit, it would solve this issue I am having.
You would need a pre-commit hook which would first modify the index by registering in it empty blob for the nested .gitignore (meaning they are not removed, just emptied). Example here.
A post-commit hook can restore the content of those .gitignore.
Solution 3:[3]
Simply add the lower nested gitignores to the top-level gitignore file:
*/**/.gitignore
That will track all files except those marked in the top-level gitignore file.
Another solution to look into may be Git subtrees or Git submodules.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | raner |
| Solution 2 | |
| Solution 3 | Zaz |
