158

I'm trying to avoid the following pattern in my .gitignore file.

MyPrject/WebApp/Scripts/special/*.js
MyPrject/WebApp/Scripts/special/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*/*/*.js
MyPrject/WebApp/Scripts/special/*/*/*/*/*/*/*.js

We tried:

MyPrject/WebApp/Scripts/special/**.js
MyPrject/WebApp/Scripts/special/**/*.js

This however didn't work. This is git on Windows. Is there a more concise way to do this without repeating things?

Dom
  • 1,687
  • 6
  • 27
  • 37
orad
  • 15,272
  • 23
  • 77
  • 113
  • 1
    Possible duplicate of [Correctly ignore all files recursively under a specific folder except for a specific file type](https://stackoverflow.com/questions/17812717/correctly-ignore-all-files-recursively-under-a-specific-folder-except-for-a-spec) – Sam Jun 12 '17 at 03:51

4 Answers4

172

As of git 1.8.2, this:

MyPrject/WebApp/Scripts/special/**/*.js

Should work according to this answer. It also works for me in Windows 7 using Sourcetree 1.6.12.0 and the version of git that it installs (1.8.4-preview20130916).

To gitignore every file and folder under a directory recursively:

MyPrject/WebApp/Scripts/special/**
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Sam
  • 3,320
  • 1
  • 17
  • 22
  • What about prior to git 1.8? – ashes999 Apr 07 '15 at 20:40
  • 1
    If this answer does not work (on Windows), add the character `/` at the beginning. Example: `/MyProject/WebApp/Scripts/special/**/*.js` – batressc Oct 26 '16 at 03:50
  • 4
    I'm embarrassed that my answer keeps getting upvotes when it is literally a link to *another* answer. – Sam Jun 16 '17 at 01:21
  • I can confirm that neither `/WebApp/Scripts/special/**/*.js` nor `*.js` works when put in `/WebApp/Scripts/special/.gitignore`, but when I put `*.js` in `/WebApp/Scripts/special/a/b/c/.gitignore` it does work. – puk Feb 04 '18 at 21:39
  • 1
    @puk : the `.gitignore` in this question was in the repo root (parent directory of `/Webapp`). – lucid_dreamer May 06 '18 at 22:33
  • 2
    Your answer looks good but didn't work for me on Windows 10 with git 1.9.1 for ignoring all files except js in following case: (first line) `foo/bar/*/*` (second line) `!foo/bar/**/*.js` It worked only for one level of subdirectories, but not recursively. – LLL Mar 05 '19 at 19:40
27

Following gitignore manual page:

[...] git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname.

So, this clearly stands that there is no way to specify a certain amount of directories between two strings, like between special and js.

Nevertheless, you can have a .gitignore file per directory, so maybe in your case the following content

*.js

at the following place

MyPrject/WebApp/Scripts/special/.gitignore

would be sufficient?

Guillaume Lemaître
  • 1,230
  • 13
  • 18
  • I used to have `MyPrject/WebApp/Scripts/special*.js` and it matched `MyPrject/WebApp/Scripts/specialfile.js` as well as `MyPrject/WebApp/Scripts/special/a/b/c/file.js`. However, for some reason the behavior has changed and my current workaround is to list directories individually. I'm using GitHub for Windows. – orad May 14 '13 at 19:41
  • 1
    e.g. `find MyPrject/WebApp/Scripts/special -type d -printf 'echo "*.js" >> %h/.gitignore\n git add %h/.gitignore\n' | sh` – jthill May 14 '13 at 22:54
26

This works for me in on osx.

lib64/**/__pycache__/
lib/**/__pycache__/
*.py[cod]
.ipynb_checkpoints/
**/.ipynb_checkpoints/
.DS_Store
**/.DS_Store
Jonathan
  • 444
  • 4
  • 5
  • I accidentally downvoted your answer and I am unable to fix it unless you edit. If you can edit it and just put a space or next line. I will fix the vote. – tomarv2 Jul 29 '21 at 19:13
5

Try executing git rm -r --cached MyPrject/WebApp/Scripts/special/ Your mentioned directory might have been cached by git and will show up on untracked files. After clearing the cache make sure you have it mentioned on .gitignore.

Pradipta Sarma
  • 1,142
  • 10
  • 17