1

Over time this large project has acquired quite a few .gitignore files scattered across its many directories. Adding a new .gitignore "at the site" is very handy when you're dealing with some new output folder or something, but it becomes hard to reason about.

Is there a way to "roll up" all these disparate .gitignore files back into the .gitignore at the root of the project? The end result would be a single .gitignore project-wide, with all the other ad-hoc ones removed.

It would be extra special if I could roll up one subtree at a time, or to a common child, so that I can manage the changes a little bit more effectively, but I won't be choosy.


Concrete example:

Before:

root
|- .gitignore
|- subdir1
|  |- .gitignore
|  |- subdir3
|     |- .gitignore
|     |- ignored_file.txt
|- subdir2
   |- .gitignore
# the .gitignore in subdir3
/ignored_file.txt

After:

root
|- .gitignore
|- subdir1
|  |- subdir3
|     |- ignored_file.txt
|- subdir2
# the .gitignore in root
/subdir1/subdir3/ignored_file.txt

Thanks for your time!

Ipsquiggle
  • 1,814
  • 1
  • 15
  • 25
  • You could write a ruby script or similar. – matt Nov 13 '20 at 04:29
  • Does this answer your question? [Can I include other .gitignore file in a .gitignore file? (like #include in c-like languages)](https://stackoverflow.com/questions/7005142/can-i-include-other-gitignore-file-in-a-gitignore-file-like-include-in-c-li) – TheGeorgeous Nov 13 '20 at 06:22
  • @TheGeorgeous Thanks, but not quite, my goal is to specifically clean up all the misc. .gitignores lying around and have only a single one remain. – Ipsquiggle Nov 16 '20 at 22:32

1 Answers1

2

The standard linux find command (or its more recent sibling fd) will list the files for you :

$ find * -name .gitignore
.gitignore
subdir1/.gitignore
subdir1/subdir3/.gitignore
subdir2/.gitignore

If you have a script that expands a .gitignore file :

# write a script, which takes a path as input,
# and prefixes all patterns found within the file with 'path/' :
$ expand.py subdir1/subdir3/.gitignore
/subdir1/subdir3/ignored_file.txt

You can combine both :

$ find * -name .gitignore | xargs -L1 expand.py
# look only in the subdir1 subdirectory :
$ find subdir1/ -name .gitignore | xargs -L1 expand.py
LeGEC
  • 46,477
  • 5
  • 57
  • 104
  • Thanks! I had started going down this path but your explanation here looks much simpler and more reliable than what I was attempting. – Ipsquiggle Nov 13 '20 at 22:08