Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in the paths in .gitignore file?

Tags:

gitignore

I've been using git but still having confusion about the .gitignore file paths.

So, what is the difference between the following two paths in .gitignore file?

 tmp/* public/documents/**/* 

I can understand that tmp/* will ignore all the files and folders inside it. Am I right? But what does that second line path mean?

like image 917
Autodidact Avatar asked Mar 25 '09 11:03

Autodidact


People also ask

What do you place within a .gitignore file?

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 . gitignore files.

Does Gitignore location matter?

gitignore file anywhere in the working directory, i.e in any folder where your code prevails. Having said that, the best practice would be to place the . gitignore file in the root directory.

Is Gitignore branch specific?

Unfortunately, git doesn't support branch-specific . gitignore files or directives. Using different .

Does Gitignore apply to subdirectories?

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


2 Answers

This depends on the behavior of your shell. Git doesn't do any work to determine how to expand these. In general, * matches any single file or folder:

/a/*/z  matches        /a/b/z  matches        /a/c/z  doesn't match  /a/b/c/z 

** matches any string of folders:

/a/**/z  matches        /a/b/z  matches        /a/b/c/z  matches        /a/b/c/d/e/f/g/h/i/z  doesn't match  /a/b/c/z/d.pr0n 

Combine ** with * to match files in an entire folder tree:

/a/**/z/*.pr0n  matches        /a/b/c/z/d.pr0n  matches        /a/b/z/foo.pr0n  doesn't match  /a/b/z/bar.txt 
like image 184
John Feminella Avatar answered Sep 28 '22 14:09

John Feminella


Update (08-Mar-2016)

Today, I am unable to find a machine where ** does not work as claimed. That includes OSX-10.11.3 (El Capitan) and Ubuntu-14.04.1 (Trusty). Possibly git-ignore as been updated, or possibly recent fnmatch handles ** as people expect. So the accepted answer now seems to be correct in practice.


Original post

The ** has no special meaning in git. It is a feature of bash >= 4.0, via

shopt -s globstar

But git does not use bash. To see what git actually does, you can experiment with git add -nv and files in several levels of sub-directories.

For the OP, I've tried every combination I can think of for the .gitignore file, and nothing works any better than this:

public/documents/

The following does not do what everyone seems to think:

public/documents/**/*.obj

I cannot get that to work no matter what I try, but at least that is consistent with the git docs. I suspect that when people add that to .gitignore, it works by accident, only because their .obj files are precisely one sub-directory deep. They probably copied the double-asterisk from a bash script. But perhaps there are systems where fnmatch(3) can handle the double-asterisk as bash can.

like image 22
cdunn2001 Avatar answered Sep 28 '22 15:09

cdunn2001



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!