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 branch several commits ago?
  • how do you organize your programming work
  • git and empty folders
  • Git - fatal: remote origin already exists
  • What's the difference between “squash” and “fixup” in Git/Git Extension?
  • Removing Git commits on a branch
  • 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?

  • Including external C++ libraries in version control
  • Check git current file version and how to handle removed files
  • VSTS: Error encountered while pushing to the remote repository: Not a valid reference 'origin/development'
  • Commit ammended on different branch
  • Git (Windows) - cannot create file on clone. Works as Superuser
  • Install DL4J on Windows
  • 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:

    *.c
    !frob_*.c
    !custom.c
    

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

    *
    !*.c
    

    ‘*’ 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

    *.*
    !*.c
    

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

    using

    *
    !*.c
    

    will not work on all version of git.

    Tested on

    git version 2.12.2.windows.2

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