Search code examples
cgitgitignore

.gitignore everything but .c and .h recursively


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 have to have these files, which should not be tracked, in the git-repo before or after i add the .gitignore?

And how do i do this?:

#ignore all
*
#but:
!source/**/*.c
!source/**/*.h

This is my current solution, but it does not work. But i think this also relates to the point in time, where i have to add the files, that should be ignored, but need to be there locally?

EDIT: The problem is, i got a copy of a project, that does all kinds of makefile magic and other things, i do not even know what kind of file-types and subfolders there are (i will only work in one folder of the massive project, so i don't think, that the gitignore needs to be so exclusive) ... and i can't just commit everything, because the "lib" has to be installed i think, so everybody needs to do this on his own ...


Solution

  • 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.