Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git-ignore certain files in sub-directories, but not all

Tags:

git

gitignore

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?

like image 861
anonymous coward Avatar asked Jul 22 '11 18:07

anonymous coward


People also ask

Does Gitignore work in subfolders?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

How do I not ignore a specific file in git?

Git ignore patterns An asterisk is a wildcard that matches zero or more characters. Prepending an exclamation mark to a pattern negates it. If a file matches a pattern, but also matches a negating pattern defined later in the file, it will not be ignored.

Does Gitignore need to be in every folder?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .

Can you have two git ignore files?

You can have multiple . gitignore , each one of course in its own directory. To check which gitignore rule is responsible for ignoring a file, use git check-ignore : git check-ignore -v -- afile .


2 Answers

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

like image 97
Ivan Danilov Avatar answered Sep 26 '22 21:09

Ivan Danilov


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

like image 35
E.Monogarov Avatar answered Sep 25 '22 21:09

E.Monogarov