Search code examples
gitbitbucketgitignore

How do i create an ignore rules exception for a specific folder in our Git repository?


At the top level of our project folder hierarchy, we've created the standard ignore rules to exclude our compiled exe's, dll's, etc. As these rules are specified at the top level of our repository, they are effective for all folders within the project hierarchy.

I have one specific folder that contains several exe's which I do want to synchronize with our Git repository. How do I turn off the exclusion at the subfolder level so that exe's within that folder will be synchronized with our Git repository? (The subfolder is a 'leaf' so once the exclusion is disabled, it can remain as such.)


Solution

  • The answer seems to be very easy:

    !*.exe
    

    ! means reverse ignore rule. See https://git-scm.com/docs/gitignore#_pattern_format:

    An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.

    You can add the rule directly in the directory (let's call it path/to/the/leaf) i.e. in the file path/to/the/leaf/.gitignore. Or you can add it in the top-level .gitignore in the form:

    !/path/to/the/leaf/*.exe
    

    Add it after ignored *.exe