Is there a way to tell git to only include certain files instead of ignoring certain files?

My programs generally generate huge output files (~1 GB) which I do not want to be backing up to the git repository. So instead of being able to do

git add .

I have to do something like

  • Git - maintain multiple versions
  • python collectstatic --noinput error when deploying Django project to Heroku
  • Tracking other peoples projects with git
  • Not able to push commit to GitHub through visual studio
  • Why no yellow in powershell and posh git
  • best way of setting up a phonegap multiplatform project repository
  • git add *.c *.cc *.f *.F *.C *.h *.cu

    which is a little bit cumbersome…

    I feel fairly confident I could write a quicky perl script ls the directory contents into .gitignore and then remove files based on a .gitinclude (or some similar name) file, but that seems a little too hackish. Is there a better way?

  • Git : Failed at pushing to remote server, ' REPOSITORY_PATH ' is not a git command
  • What is resetting the PATH variable at the last second during an OpenShift v2 push hook?
  • How to solve'fatal: index-pack failed'?
  • Git does not ask me for gpg passphrase and fails my commit
  • Mysysgit “(END)” displays, how do I proceed?
  • Fix license in old Git commits
  • 3 Solutions collect form web for “Is there a way to tell git to only include certain files instead of ignoring certain files?”

    I haven’t had need to try this myself, but from my reading of TFM it looks like a negated pattern would do what you want. You can override entries in .gitignore with later negated entries. Thus you could do something like:


    To have it ignore all .c files except custom.c and anything starting with “frob_”

    create .gitignore file in your repository and you want to track only c files and ignore all other files then add the following lines to it….


    ‘*’ will ignore all files

    and ! will negate files be to ignored….so here we are asking git not to ignore c files….

    The best solution to achieve this

    create .gitignore file in repository root, and if you want to include only .c file then you need to add below lines to .gitignore file


    this will include all .c file from directory and subdirectory recursively.



    will not work on all version of git.

    Tested on

    git version

    Git Baby is a git and github fan, let's start git clone.