问题
I found a below sentence in Git 2.7 Release Notes.
Allow a later "!/abc/def" to override an earlier "/abc" that appears in the same .gitignore file to make it easier to express "everything in /abc directory is ignored, except for ...".
Git 2.7 Release Notes
However, This function seems to be invalid in subsequent versions.
$ git --version
git version 2.19.1
$ cat .gitignore
/abc
!/abc/def
$ mkdir abc
$ echo foo > abc/def
$ git status
On branch master
nothing to commit, working tree clean
Did Git change the "!" prefix behavior at .gitignore? Or did I mistake?
回答1:
It looks like that change was reverted in 2.8.0, why are you combing two-year-old release notes to see how gitignore behaves when you could just say git help ignore
and it'd tell you
- 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. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt".
The easy way to do your gitignores if you don't want the ignored-directory pruning is to ignore their entire contents with
directory/**
follwed by any overrides, (e.g. your !directory/def
) and at the end of your gitignore put
!*/
followed by any complete exclusions you really meant for effect.
来源:https://stackoverflow.com/questions/53753201/did-git-change-the-prefix-behavior-at-gitignore