Search code examples
gitgithubgitignore

How can I make gitignore apply to a certain extension except those a certain folder?


Our company has our projects in GitHub in a private organization. One of these projects, in Delphi, has a number of third-party libraries associated with it, contained in the same repository.

The nature of Delphi is that a .PAS file produces a .DCU file (compiled), and by nature, .DCU files are one of those things we don't want in GitHub (unnecessary and takes up space). In fact, when generating a new default .gitignore file for "Pascal", .DCU is among the things it ignores by default.

However, one of the third-party libraries does not include the original source (.PAS), but instead only the compiled (.DCU) files. So when we tried to clone this repo on another machine, that library was missing pretty much all the files it needed.

I found a few other questions here on SO similar to this topic, but those typically referred to ignoring everything except certain things. I tried this:

!Path\to\a\folder\I\still\want\dcu\files\from\*.dcu

But it is still ignoring these files.

How can I make the git ignore file exclude all DCU files except for those in a particular directory and its subdirectories?

Note: This directory has many subdirectories, each of which has DCU files I want. So I'd like this to be recursive rather than listing each folder individually.


Solution

  • You can write a .gitignore file like this:

    *.dcu
    !project/compiled-only/**/*.dcu
    

    This excludes all of the .dcu files, except those in project/compiled-only or any subdirectory.

    Note that you must use forward slashes on all operating systems, since backslashes are used as escape characters.