How do I Gitnore all files except two subdirectories?

我的未来我决定 提交于 2019-12-01 02:04:05

Ok I found a way, but this is completely ridiculous! This way will list untracked files if a new file is added.

# Ignore everything in src/ except wp-content/
src/*
!src/wp-content/
# Ignore everything in wp-content/ except themes/
src/wp-content/*
!src/wp-content/themes/
# Ignore everything in themes/ except for these 2 themes
src/wp-content/themes/*
!src/wp-content/themes/chocolat-child/
!src/wp-content/themes/othertheme
Landys

Actually you can make it a bit more graceful. The following should work for you. Just remember for directories, you need to add ** at the end of the pattern to include all files under it but not only the directory itself back.

src/**
!src/**/
# for directories
!src/wp-content/themes/chocolat-child/**
# for files
!src/wp-content/themes/othertheme

If you want to also ignore all files/directories outside src directory, make it as follows.

*
!*/
# for directories
!src/wp-content/themes/chocolat-child/**
# for files
!src/wp-content/themes/othertheme

To understand the reasons, please refer to my answer to a SO question. In general, there're 2 rules for the negating pattern in .gitignore.

Rule 1. Files and directories are separatedly handled in the patterns. To include a directory back doesn't mean its child files/directories are also included back.

Rule 2. It won't include files/directories back if their parent directory is still ignored.

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