Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git be made to ignore nested .gitignore files when computing ignores?

Tags:

git

gitignore

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?

like image 753
OrangeDog Avatar asked Sep 25 '22 15:09

OrangeDog


2 Answers

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
like image 122
raner Avatar answered Oct 11 '22 06:10

raner


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 .gitignore file 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 .gitignore files

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 .gitignore b4 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.

like image 38
VonC Avatar answered Oct 11 '22 07:10

VonC