Search code examples
gitgitignore

Git is ignoring a file in a subdirectory


I have a .gitignore file that looks like this:

*
!/src
!/resources
!/.gitignore
!/package.json
!/tsconfig.json
!/README.md

I added a file to a subdirectory in the src folder src/config/mongo.ts. However, when I try to add the file it doesn't get added:

git add -A

I have tried modifying the second line to each of the following:

!/src/
!/src/*
!/src/**/*

However it still doesn't add the file.

I have also tried adding the file directly:

git add src/config/mongo.ts

But I get the following message:

The following paths are ignored by one of your .gitignore files: src/config/mongo.ts

git status only shows that the .gitignore file has been changed. Not sure what to do to get this working.

I would like to:

  • First exclude everything *
  • Then include everything in the src directory (and its subdirectories)
  • Then include everything in the resources directory (and its subdirectories)
  • Then include the listed root files

Solution

  • This one is a tricky one, took me forever to figure out when I originally ran into it.

    You just need to soften the initial exclusion slightly:

    /* # instead of just *