简体   繁体   中英

How to ignore to level directory in Git but not other directories starting with same name?

How to ignore top level cache but not 'apps/cache'?

I have such structure.

cache
apps/cache
conf/cache

I want to write rules to ignore only top level cache .

cache - IGNORE
apps/cache - NOT IGNORE
conf/cache - NOT IGNORE

How to do in git?

Just prefix the directory to be ignored with /. This will ignore the mentioned files/directories only in the directory where .gitignore is present.

eg: /cache

Just provide the full path to your cache directory:

/cache

This way, things like app/cache and conf/cache won't be included.

Note: the ability to anchor the path to the current folder of a git repo comes from commit d0bfd02 (April 2007, git 1.5.2-rc0) :

Add basic infrastructure to assign attributes to paths

This adds the basic infrastructure to assign attributes to paths, in a way similar to what the exclusion mechanism does based on $GIT_DIR/info/exclude and .gitignore files.

Each line in these files defines a pattern matching rule.

The first token on the line is a shell glob pattern.

For glob matching, the same "if the pattern does not have a slash in it, the basename of the path is matched with fnmatch(3) against the pattern, otherwise, the path is matched with the pattern with FNM_PATHNAME" rule as the exclusion mechanism is used.

That is why the documentation soon included (June 2007, git 1.5.2.1):

A leading slash matches the beginning of the pathname;
for example, " /*.c " matches " cat-file.c " but not " mozilla-sha1/sha1.c "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM