LoranceChen
LoranceChen

Reputation: 2574

git ignore /* and !/**/somefile* not exclude properly

my git repo directory like this:

--ignore_dir/
----dir2/
------ignorea3.txt
----aaa.txt
----ignorea2.txt
--ignorea1.txt
--ignore1.txt
--.gitignore

.gitignore file like this:

/*
!/**/ignorea*.txt

with git status, it output:

On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ignorea1.txt

nothing added to commit but untracked files present (use "git add" to track)

My question is why it not tracking files: ignore_dir/dir2/ignorea3.txt and ignore_dir/ignorea2.txt

Thanks!

UPDATE
Thanks @RomainValeri and @torek,
I got a complex way to achieve this, by edit gitignore file:

/*
!/ignore_dir/
/ignore_dir/*
!/ignore_dir/dir2/

/ignore_dir/dir2/*
!/ignore_dir/dir2/ignorea*.txt
!/ignore_dir/ignorea*.txt

It give a explicit tracking every parent level directory of the ignorea*.txt.

Besides, this will not work:

/*
!**/ignorea*.txt

Hope someone could give a elegant way!

Upvotes: 1

Views: 53

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 21908

From the doc :

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded.

(emphasis mine)

So, since directories ignore_dir and dir2 are excluded by the first pattern, reincluding files inside them in the next line won't work.


Failed attempt :

# ignore eveything
/*

# reintroduce "ignore_dir" directory
!/ignore_dir

# reintroduce "dir2" directory
!/ignore_dir/dir2

# reintroduce any ignorea*.txt file in any directory
!/**/ignorea*.txt

But as torek mentions below, this isn't working. To be improved.

Upvotes: 1

Related Questions