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

GitGitignore

Git Problem Overview


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 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 Solutions


Solution 1 - Git

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_"

Solution 2 - Git

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

Solution 3 - Git

> 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

Solution 4 - Git

If you need to ignore files but not a specific file inside a directory, here is how I did it:

# Ignore everything under "directory"
directory/*
# But don't ignore "another_directory"
!directory/another_directory
# But ignore everything under "another_directory"
directory/another_directory/*
# But don't ignore "file_to_be_staged.txt"
!directory/another_directory/file_to_be_staged.txt

Solution 5 - Git

Late to the party, but my solution would be to have a directory for source files and a different directory for executables and program output, something like this:

+ .git
|    (...)
+ bin
|    my_exe.exe
|    my_output.txt
+ src
     some_file.c
     some_file.h 

... and then only add the stuff in src/ to my repository and ignore bin/ entirely.

Solution 6 - Git

If you're only trying to include dot files, this worked for me...

!.*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaisy Sophia HollmanView Question on Stackoverflow
Solution 1 - GitT.E.D.View Answer on Stackoverflow
Solution 2 - GitVineel Kumar ReddyView Answer on Stackoverflow
Solution 3 - GitSmaranjitView Answer on Stackoverflow
Solution 4 - GitgzfranciscoView Answer on Stackoverflow
Solution 5 - GitChristian SeverinView Answer on Stackoverflow
Solution 6 - Gitcopeland3300View Answer on Stackoverflow