threads / discuss / 16757

git ignore

Subject: git ignore

## tl;dr

7 messages between Dec 16, 2008 and Jul 8, 2011.

replies: 6people: 5as markdown or json

Max Power· Dec 16, 2008, 22:43 UTC · lore

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· Dec 16, 2008, 22:53 UTC · re: Max Power · lore

Re: git ignore

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· Dec 16, 2008, 23:08 UTC · re: Linus Torvalds · lore

Re: git ignore

Linus Torvalds <torvalds@linux-foundation.org> writes:
Show 12 quoted lines
> 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· Dec 18, 2008, 17:11 UTC · re: Junio C Hamano · lore

[PATCH] test overlapping ignore patterns

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· Jul 7, 2011, 18:12 UTC · re: Michael J Gruber · lore

Re: [PATCH] test overlapping ignore patterns

> 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· Jul 8, 2011, 18:01 UTC · re: Stephen Haberman · lore

Re: [PATCH] test overlapping ignore patterns

Stephen Haberman <stephen@exigencecorp.com> writes:
Show 6 quoted lines
> +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· Jul 8, 2011, 18:39 UTC · re: Junio C Hamano · lore

Re: [PATCH] test overlapping ignore patterns

> 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

← back to recent threads