How do I Gitnore all files except two subdirectories?

后端 未结 2 1587
渐次进展
渐次进展 2021-01-07 08:48

How do I ignore all files in a project except two subdirectories? I don\'t want to include all of Wordpress in Git, but I do want to include the customized themes. I have tw

2条回答
  •  一整个雨季
    2021-01-07 09:27

    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.

提交回复
热议问题