How do negated patterns work in .gitignore?

风格不统一 提交于 2019-11-26 05:29:33

问题


I am attempting to use a .gitignore file with negated patterns (lines starting with !), but it\'s not working the way I expect.

As a minimal example, I have the folllowing directory structure:

C:/gittest
 -- .gitignore
 -- aaa/
   -- bbb/
     -- file.txt
   -- ccc/
     -- otherfile.txt

and in my gitignore file, I have this:

aaa/
!aaa/ccc/

My understanding (based on this doc page) is that the file aaa/ccc/otherfile.txt should not be ignored, but in fact git is ignoring everything under aaa.

Am I misunderstanding this sentence: \"An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again.\"?

BTW, this is on Windows with msysgit 1.7.0.2.


回答1:


I think that what you actually want to do is:

aaa/*
!aaa/ccc

You're telling it "don't look in aaa" so it never even examines the path aaa/ccc. If you use the wildcard, it still reads the contents of aaa, then each entry matches the wildcard and is ignored, except aaa/ccc which gets put back in.




回答2:


If you want to exclude everything in aaa, but include aaa/ccc and everything beneath it, you should use:

aaa/*
!aaa/ccc
!aaa/ccc/*

The first line tells git to ignore everthing beneath aaa, the second tells it not to ignore the folder aaa/ccc which actually "enables" the third line which then tells it not to ignore everything beneath aaa/ccc.




回答3:


If anyone's still not seeing newly un-ignored items in a git status running a git update-index before hand can help git to see the changes (at least in version 1.9.x of gitbash).



来源:https://stackoverflow.com/questions/2820255/how-do-negated-patterns-work-in-gitignore

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