Search code examples
gitgitignore

Ignore everything but specific filetype


I'm setting up a git repository, But I only want specific types to be added to git. For example I'll have the following files:

/door/index.md
/door/photo.png
/door/video.mp4

Via .gitignore, I only want the index.md and photo.png files to be synced, Anything else needs to be ignored. I tried the following, but it doesn't seem to work:

/*
/*/
!.gitignore
!/**/*.md
!**/*.md
!*.md

Solution

  • Quoting from git help ignore (emphasis mine):

    An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded.

    So that's what happens, everything is ignored per * and later !s have no effect.

    Having said that, you can easily debug your .gitignore using git check-ignore -v path/to/file