peko
peko

Reputation: 11335

gitignore match files with pre and suffix in subdirectory

I like to track only files that start with "es" and end with *.h or *.m

so i tried this:

#exlude all
*
#except
!es*.h
!es*.m

and

#exlude all
*
#except
!*/es*.h
!*/es*.m

but neither works for files in subdirectories

Upvotes: 3

Views: 1600

Answers (1)

c00kiemon5ter
c00kiemon5ter

Reputation: 17604

When you ignore everything (*), you ignore folders, even if they have contents too, as * matches everything.

If you unignore something, that would be matched only for the files of the root dir. If you need to unignore a directory, you need to explicitly say that (ie !mydir/). But that would unignore all the contents of that dir, so you'd have to redefine the ignore/unignore pattern for the contents of that dir. Even if you do that, if you haven't already added the dir to the index, you won't see that in git status.

Your case can be solved easily but inverting the pattern.
What you basically want to do is ignore everything that

  • does not start with es and
  • does not end with .h or .m.

So do that:

$ ls -a
.  ..  .git  .gitignore  a  b  blah  c  ebar.m  esfoo.h  esfoo.m  sbar.m
$ ls -a blah/
.  ..  a  b  c  ebar.m  esfoo.h  esfoo.m  sbar.m
$ git status -s
?? blah/
?? esfoo.h
?? esfoo.m
$ git status -s blah/   # matching files ignored and also ignored on `git add`
?? blah/esfoo.h
?? blah/esfoo.m
$ git add .
$ git status -s         # only wanted files were added
A  blah/esfoo.h
A  blah/esfoo.m
A  esfoo.h
A  esfoo.m
$ cat .gitignore        # the ignore pattern -- ignore
[^e]*                   # everything that doesn't start with 'e'
e[^s]*                  # and is not followed by an 's'
*.[^hm]                 # and does not end with '.h' or '.m'
!/blah                  # uningore the wanted subdirs

As you see in the last command, I've inverted your pattern to ignore everything that doesn't start with e and is not followed by an s and that doesn't end with .h or .m and also unignored a dir. Even though the dir had more content, it was ignored as it matched the pattern, and only the wanted parts were added.

edit: updated

Upvotes: 2

Related Questions