How to ignore a directory in git while excluding some sub-directories and files?

These days git is one of the most used code tracking software. It uses a file called “.gitignore” to determine the files to be tracked and ignored. If this file is not filled with the correct statements then we might end up losing some of the valuable code while tracking the unwanted files. In this article, I’m going to show how we can un-ignore some subdirectories and files of a directory that is being ignored.

Directory structure:

  • includethis
  • ignorethis
    • includethis
    • ignored.file
    • file.notignored

In the above directory structure, the directories highlighted in red color are being ignored while the directories and files highlighted in the green color are being tracked. To achieve this we can use any of the following “.gitignore” files.

Example 1

ignorethis/*
!ignorethis/includethis
!ignorethis/*.notignored

Example 2

ignorethis/*
!ignorethis/includethis
!ignorethis/file.notignored

Here the important thing to note is that we are putting the `ignorethis/*` in both files because if we don't put `/*`. Then any statement for tracking anything inside that directory will not be considered by Git.

Leave a Reply

Your email address will not be published.