1

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!

LoranceChen
  • 2,453
  • 2
  • 22
  • 48

1 Answers1

1

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.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • hi, thanks. It's there are standard way to hand such situation, tracking all `ignorea*.txt`? – LoranceChen Mar 21 '19 at 14:19
  • @LoranceChen Been trying everything in the last half hour, but in vain. I'm sorry not having found, but I thank you a lot for all I learned during the process :-) And I'll be very happy to see what others will propose to solve it. – Romain Valeri Mar 21 '19 at 14:45
  • Remember that Git is doing a recursive tree walk of all the directories in your work-tree. For all *other* operations (not for .gitignore) Git just says to itself: *oh, subdirectory, look inside because all I care about is files*. But for matching a `.gitignore` rule Git tries the subdirectory itself: if it matches, it *doesn't bother scanning the subdirectory at all* and hence never recurses into it to find files in it that would be un-ignored. You can defeat this with a simple `!**/` rule if you put it in the right place, but this does completely kill off the optimization. – torek Mar 21 '19 at 16:14