Exceptions in .gitignore [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-30 06:11:51

问题


How can I add an exception to .gitignore, like "ignore all the .dll files BUT myfile.dll"?


回答1:


Use ! to negate the pattern:

*.dll
!myfile.dll



回答2:


If you want to ignore whole folder, except some specific files, then write:

MyFolder/*
!MyFolder/CoolFile.txt

This won't work:

MyFolder/
!MyFolder/CoolFile.txt



回答3:


You can also ignore folders like

!src/main/resources/archetype-resources/**/*

you can also ignore nested folder with patterns like

!**/src/test/resources/**/*



回答4:


You can have several .gitignore files working together in a hierarchical manner to achieve your goal. At the root level you may have:

root

*.dll

inside the folder having the myfile.dll you can add another .gitignore file like so:

root/lib/folderwithMyFiledll

!myfile.dll

more info here

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt". It is possible to re-include a file if a parent directory of that file is excluded if certain conditions are met. See section NOTES for detail.




回答5:


I did this because I have a folder called /modules that I want to ignore, except everything in the /modules/custom folder. This worked for me. Now my custom modules will get synced to GitHub.

/modules/*
!/modules/custom/


来源:https://stackoverflow.com/questions/2415873/exceptions-in-gitignore

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