gitignore match files with pre and suffix in subdirectory

牧云@^-^@ 提交于 2019-12-10 21:28:23

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/8850203/gitignore-match-files-with-pre-and-suffix-in-subdirectory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!