How do I Gitnore all files except two subdirectories?

后端 未结 2 1588
渐次进展
渐次进展 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:20

    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题