.gitignore everything but .c and .h recursively

前端 未结 2 1228

I would like to ignore everything in a certain folder and its subfolders, except for .c and .h files.

Yet locally, i need other files too. Do i

相关标签:
2条回答
  • 2021-01-29 02:35

    Ignoring * means ignore everything including top-level directories. After that git doesn't even look into subdirectories. To fix that unignore directories. Your entire .gitignore should look like this:

    # Ignore all
    *
    
    # Unignore directories
    !*/
    
    # Unignore source code files
    !source/**/*.c
    !source/**/*.h
    

    Another approach is to ignore everything but force-add necessary files with git add -f.

    0 讨论(0)
  • 2021-01-29 02:53

    The problem is that the pattern

    *
    

    excludes all directories, too. According to the gitignore documentation,

    It is not possible to re-include a file if a parent directory of that file is excluded.

    To make this work, then, you'll need to use make sure that directories are not ignored. The gitignore pattern format does not provide a way to distinguish between directories and regular files, so you'll need to do that manually. One possibility would be to put a .gitignore file in each that directory that reincludes all its subdirectories, but it would be easier to just reinclude all directories. These can be matched (exclusively) with a pattern that ends with a '/':

    !source/**/
    

    Also, you are right when you say

    But i think this also relates to the point in time, where i have to add the files, that should be ignored

    in the sense that gitignore does not apply to files that are already tracked.

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