Search code examples
gitgitignore

How to make gitignore exclude a specific js file present in 2 folders, whereas ignoring all other js files?


File Structure:

  • Root
    • Folder1
      • file.js
      • more files
    • Folder2
      • file.js
      • more files
    • .gitignore

I want to ignore all .js files except file.js(which is present in both folders with same name). So, in my gitignore, I tried the following:

Option 1:

!*file.js
*.js

Option 2:

!/*/file.js
*.js

Option 3:

!/Folder1/file.js
!/Folder2/file.js
*.js

But always only the file.js in Folder1 is included with all 3 options, and file.js in Folder2 is always ignored. Other js files in both folders are ignored perfectly. What am I doing wrong here?


Solution

  • You want the global *.js at the top. By putting *.js at the bottom it overrides the excludes above it.

    *.js
    !Folder1/file.js
    !Folder2/file.fs