# git ignore

7 messages from 2008-12-16 to 2011-07-08. Participants: Max Power, Linus Torvalds, Junio C Hamano, Michael J Gruber, Stephen Haberman.
Thread: https://gitlist.dev/t/16757

## Max Power, 2008-12-16 22:43

Subject: git ignore
Message-ID: <21043430.post@talk.nabble.com>
URL: https://gitlist.dev/e/21043430.post%40talk.nabble.com

```

So I understand how to use the .gitignore file to ignore specific
files/directories that I put in there... is there a way to ignore everything
BUT a given file extension? 
-- 
View this message in context: http://www.nabble.com/git-ignore-tp21043430p21043430.html
Sent from the git mailing list archive at Nabble.com.

```

## Linus Torvalds, 2008-12-16 22:53

Subject: Re: git ignore
Message-ID: <alpine.LFD.2.00.0812161450010.14014@localhost.localdomain>
URL: https://gitlist.dev/e/alpine.LFD.2.00.0812161450010.14014%40localhost.localdomain
In-Reply-To: <21043430.post@talk.nabble.com>

```


On Tue, 16 Dec 2008, Max Power wrote:
> 
> So I understand how to use the .gitignore file to ignore specific
> files/directories that I put in there... is there a way to ignore everything
> BUT a given file extension? 

Something like

	*
	!*.jpg

to only save the jpegs in your pr0n collection?

The first rule says "ignore everything". The second one says "don't 
ignore *.jpg files".

Untested. 

		Linus

```

## Junio C Hamano, 2008-12-16 23:08

Subject: Re: git ignore
Message-ID: <7viqpjra57.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7viqpjra57.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <alpine.LFD.2.00.0812161450010.14014@localhost.localdomain>

```
Linus Torvalds <torvalds@linux-foundation.org> writes:

> On Tue, 16 Dec 2008, Max Power wrote:
>> 
>> So I understand how to use the .gitignore file to ignore specific
>> files/directories that I put in there... is there a way to ignore everything
>> BUT a given file extension? 
>
> Something like
>
> 	*
> 	!*.jpg
>
> to only save the jpegs in your pr0n collection?

Hmm, do people still keep p0rn collection in jpgs?  I somehow had an
impression that they moved to avis ;-)

> The first rule says "ignore everything". The second one says "don't 
> ignore *.jpg files".
>
> Untested. 

t3001-ls-files-others-exclude.sh has some tests but there is nothing that
explicitly tests overlapping patterns.  Perhaps it should (hint, hint...)

"man gitignore" documentation has an example to ignore all *.html files
but not foo.html using a similar construct, i.e.

	*.html
        !foo.html

```

## Michael J Gruber, 2008-12-18 17:11

Subject: [PATCH] test overlapping ignore patterns
Message-ID: <1229620278-25152-1-git-send-email-git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/1229620278-25152-1-git-send-email-git%40drmicha.warpmail.net
In-Reply-To: <7viqpjra57.fsf@gitster.siamese.dyndns.org>

```
Add a test which checks that negated patterns such as "!foo.html" can
override previous patterns such as "*.html". This is documented
behaviour but had not been tested so far.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---
 t/t3001-ls-files-others-exclude.sh |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
index 8666946..85aef12 100755
--- a/t/t3001-ls-files-others-exclude.sh
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -140,4 +140,10 @@ test_expect_success 'trailing slash in exclude forces directory match (2)' '
 
 '
 
+test_expect_success 'negated exclude matches can override previous ones' '
+
+	git ls-files --others --exclude="a.*" --exclude="!a.1" >output &&
+	grep "^a.1" output
+'
+
 test_done
-- 
1.6.1.rc1

```

## Stephen Haberman, 2011-07-07 18:12

Subject: Re: [PATCH] test overlapping ignore patterns
Message-ID: <1310062372893-6559395.post@n2.nabble.com>
URL: https://gitlist.dev/e/1310062372893-6559395.post%40n2.nabble.com
In-Reply-To: <1229620278-25152-1-git-send-email-git@drmicha.warpmail.net>

```

> Add a test which checks that negated patterns such as "!foo.html" can 
> override previous patterns such as "*.html". This is documented 
> behaviour but had not been tested so far. 

Negated excludes with a pattern does not seem to work with sub directories,
e.g. the following test fails for me.

The file /one/two/a.1 exists, and is initially excluded by *, but not
re-included by "!*.1" (where as /a.1 is re-included).

diff --git i/t/t3001-ls-files-others-exclude.sh
w/t/t3001-ls-files-others-exclude.sh
index c8fe978..e6c86ff 100755
--- i/t/t3001-ls-files-others-exclude.sh
+++ w/t/t3001-ls-files-others-exclude.sh
@@ -175,6 +175,12 @@ test_expect_success 'negated exclude matches can
override previous ones' '
  grep "^a.1" output
 '
 
+test_expect_success 'negated exclude matches can override previous ones 2'
'
+
+ git ls-files --others --exclude="*" --exclude="!*.1" >output &&
+ grep "^one/two/a.1" output
+'
+

(Replying to this old thread via nabble; hopefully it works.)

- Stephen


--
View this message in context: http://git.661346.n2.nabble.com/git-ignore-tp1665336p6559395.html
Sent from the git mailing list archive at Nabble.com.

```

## Junio C Hamano, 2011-07-08 18:01

Subject: Re: [PATCH] test overlapping ignore patterns
Message-ID: <7vk4bsfy8c.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vk4bsfy8c.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1310062372893-6559395.post@n2.nabble.com>

```
Stephen Haberman <stephen@exigencecorp.com> writes:

> +test_expect_success 'negated exclude matches can override previous ones 2'
> '
> +
> + git ls-files --others --exclude="*" --exclude="!*.1" >output &&
> + grep "^one/two/a.1" output
> +'

Does this has anything to do with "negated overrides"?

Doesn't the first "*" tells us to not to look at the contents of one/
directory at all, so we that we do not have to waste time descending into
it?  We wouldn't even know what pathnames are underneath it, so "!*.1"
would not have any effect inside one/ at all, I think.

```

## Stephen Haberman, 2011-07-08 18:39

Subject: Re: [PATCH] test overlapping ignore patterns
Message-ID: <20110708133930.04b04a49@sh9>
URL: https://gitlist.dev/e/20110708133930.04b04a49%40sh9
In-Reply-To: <7vk4bsfy8c.fsf@alter.siamese.dyndns.org>

```

> Doesn't the first "*" tells us to not to look at the contents of one/
> directory at all, so we that we do not have to waste time descending
> into it?  We wouldn't even know what pathnames are underneath it, so
> "!*.1" would not have any effect inside one/ at all, I think.

If that's the way the feature should work, that's fine.

I came across this thread via google referencing Linus's post from 2008
which mentioned versioning only jpg files. I want to do the same sort of
thing, but with a directory hierarchy.

If I say "exclude *.jpg" in my root .gitignore file, that applies into
sub directories, so I just assumed "exclude !*.jpg" would also apply to
sub directories.

Am I missing another way to accomplish this?

I understand from an implementation perspective, skipping all
subdirectories when you see "exlude *" is a smart thing to do, but
perhaps it should only be done if there are no negative excludes that
could bring items back into play?

Thanks,
Stephen

```
