Anders Lindén Anders Lindén - 1 year ago 104
Git Question

How do I clean up the .gitignore file?

Is there git command that can be used to remove lines in .gitignore that are not used?

In other words, I want to remove patterns that will not make a difference for the project.

Answer Source

There is nothing built in to Git to do this, so you would have to write your own code, probably using git check-ignore. Such a program would be quite complex since some ignore entries might be unused because of other existing ignore entries. As a somewhat silly example, if one entry says *.a, one says *.b, and a third says *.[ab], removing either or both of the first two is "safe" (makes no change in what gets ignored) as long as the third remains, but removing the third requires keeping both of the first two. This means that finding a truly minimal set of ignore entries would require trying all combinations.

In any case, it's not a good idea in general. Suppose, for instance, that the project .gitignore contains these two lines (among others):


and that you are working on Linux—but the project, which builds a shared, dynamically loaded library, is not limited to Linux.

These lines are present for, and used by, those working on Macs (the first line) and Windows (the second one). You will never use them yourself. Your system builds neither DLLs nor Dylib files; it builds shared library (*.so) files instead; and any automatic code you build that tests whether you use them will say: no, these are not used. So you might remove them ... and cause trouble for other developers. If they ran your program, it would delete the *.so line that you are using.

(This happens with IDEs as well, which may create files or directories with "hidden" names that Git can see. If you use a different IDE from Bob, and Carol uses a third, you may all three need different entries. It's possible to work around this using your own global "excludes" file, and that's usually the right answer if your editor makes backup files: if Bob's editor makes *~ while Carol's makes #*, Bob can put *~ in his personal global exclude, and Carol can put #* in hers. Meanwhile if you use vim, you would put .swp and *.swp in yours. But this also tends to be project-dependent sometimes.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download