Ignoring everything in .gitignore and adding subfo

2019-05-31 19:06发布

I have the following .gitignore:

# Ignore everything
/*
!/.gitignore

# Readd folders:
#!/mainfolder/ # this works!
!/mainfolder/subfolder/
!/mainfolder/subfolder/*

I want to ignore everything, but subfolder/. Adding the line !/mainfolder/ works, so that /mainfolder is not ignored anymore. But I want to add only the things below subfolder/ and the second and third lines for subfolder/ do not work.

I have found several links online suggesting this is the correct way (e.g. this one). So what am I doing wrong?!

EDIT: I am using Git 2.17.1 from SourceTree on Windows 10. I have tested these results from the integrated MINGW64 bash.

标签: git gitignore
1条回答
对你真心纯属浪费
2楼-- · 2019-05-31 19:18

You need to whitelist folders first, then files:

*
!.gitignore
!/mainfolder/
/mainfolder/*
!/mainfolder/subfolder/
!/mainfolder/subfolder/**

For any file still ignored, check why with:

git check-ignore -v -- path/to/ignored/file

would it be possible to extend the answer on how you would do this for a deeper subfolder? E.g. how would I ignore everything except ´deepsubfolder´(?): !/mainfolder/subfolder/deepsubfolde

That is similar to "Git - Unignore any folder located anywhere inside repository that recursively allows everything":

*
!.gitignore
!*/
!**/deepsubfolder/**
查看更多
登录 后发表回答