Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do negated patterns work in .gitignore?

Tags:

git

gitignore

I am attempting to use a .gitignore file with negated patterns (lines starting with !), but it's not working the way I expect.

As a minimal example, I have the folllowing directory structure:

C:/gittest  -- .gitignore  -- aaa/    -- bbb/      -- file.txt    -- ccc/      -- otherfile.txt 

and in my gitignore file, I have this:

aaa/ !aaa/ccc/ 

My understanding (based on this doc page) is that the file aaa/ccc/otherfile.txt should not be ignored, but in fact git is ignoring everything under aaa.

Am I misunderstanding this sentence: "An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again."?

BTW, this is on Windows with msysgit 1.7.0.2.

like image 785
Chris Perkins Avatar asked May 12 '10 15:05

Chris Perkins


People also ask

Does Gitignore support regex?

No, gitignore doesn't support regex es, it only supports unix fnmatch style patterns.

How does .gitignore work?

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 .

Does Gitignore ignore tracked files?

gitignore will prevent untracked files from being added (without an add -f ) to the set of files tracked by Git. However, Git will continue to track any files that are already being tracked.

Does Gitignore ignore subdirectories?

If the pattern doesn't start with a slash, it matches files and directories in any directory or subdirectory. If the pattern ends with a slash, it matches only directories. When a directory is ignored, all of its files and subdirectories are also ignored.


2 Answers

I think that what you actually want to do is:

aaa/* !aaa/ccc 

You're telling it "don't look in aaa" so it never even examines the path aaa/ccc. If you use the wildcard, it still reads the contents of aaa, then each entry matches the wildcard and is ignored, except aaa/ccc which gets put back in.

like image 120
Cascabel Avatar answered Oct 23 '22 13:10

Cascabel


If you want to exclude everything in aaa, but include aaa/ccc and everything beneath it, you should use:

aaa/* !aaa/ccc !aaa/ccc/* 

The first line tells git to ignore everthing beneath aaa, the second tells it not to ignore the folder aaa/ccc which actually "enables" the third line which then tells it not to ignore everything beneath aaa/ccc.

like image 34
xmak Avatar answered Oct 23 '22 13:10

xmak