87

I have a structure similar to the following:

/root/
/root/data/
/root/data/script.php
/root/data/some.json
/root/data/feature/one.json
/root/data/feature/two.json
/root/data/other-feature/one.json
/root/data/other-feature/important-script.php

I'd like git to ignore any .json files under the '/data/...' path, but '/data/' sometimes contains sub-directories.

My understanding is that a simple data/*.json in gitignore will only match one directory, as the * doesn't match /, as stated at http://git-scm.com/docs/gitignore, "Pattern Format", bullet #6:

Otherwise, 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. For example, "Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".

Is there a simple way to do this, or do I need to actively add gitignore files in each sub-directory, explicitly?

anonymous coward
  • 12,594
  • 13
  • 55
  • 97

4 Answers4

114

I've written a post about such problem recently. See here.

Basically what you need is to put one .gitignore with *.json in the /data/ directory.

UPD: Since git 1.8.4 (1.8.2 if you're using msysgit) it is possible to use double-star patterns, like /data/**/*.json

Ivan Danilov
  • 14,287
  • 6
  • 48
  • 66
  • I added the `.gitignore` file, containing `*.json`, but if I change one of those files, and run `git status`, I see them listed as "Changes not staged". Do `.gitignore` files need to committed before taking effect? Is this showing up because the files were previously tracked? – anonymous coward Jul 22 '11 at 20:12
  • 19
    Previously tracked files will be tracked no matter what is in `.gitignore` – Ivan Danilov Jul 22 '11 at 20:17
  • 1
    Back up a file you want to stop tracking, then try `git rm --cached `. This will remove the file from remote, while `--cached` will retain it locally. I think the file will no longer appear under "Changes not staged." If that works, you can do the same for all others: `git rm --cached `. – user3897315 Feb 17 '22 at 22:24
  • If you want to ignore **all except php**, you need `/data/**/*.*` followed by `/data/**/*.php`. – Mark Loyman Sep 29 '22 at 08:58
  • 1
    @MarkLoyman I guess you meant "followed by `!/data/**/*.php`" to un-ignore php files back. – Ivan Danilov Oct 12 '22 at 20:37
  • @IvanDanilov Yes, thank you for the correction. – Mark Loyman Oct 16 '22 at 08:44
26

you can place data/**/*.json in your .gitignore in /root directory to prevent multiple .gitignore files in different directories

**/ - matches any count of subdirectories (including current)

example: data/**/*.json record will ignore data/1.json, data/subfolder/2.json, data/../../3.json

E.Monogarov
  • 460
  • 8
  • 17
  • @CaptainBlammo I'm using it, it works, may be you did somthing wrong? there is info: [some docs](http://git-scm.com/docs/gitignore) – E.Monogarov May 14 '15 at 10:53
  • 1
    and it will also ignore `/bla/data/**/*.json` and `/bla/bla/data/**/*.json`. `/data/**/*.json` should be used. – Onur Demir Jun 27 '19 at 22:19
5

This pattern work for me for data subfolder ignoring only png and jpg files:

**/data/**/*.png
**/data/**/*.jpg
Aqib Mumtaz
  • 4,936
  • 1
  • 36
  • 33
0

Quick Answer

# ignore all nested json file
/**/*.json
myworldbox
  • 361
  • 6
  • 11