225

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?

7 Answers 7

317

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

11
  • 30
    Thanks, T.E.D. This worked. All I had to do was start the .gitignore file with * and then list all of my included file patterns proceeded by an exclamation point. Commented Aug 14, 2009 at 19:44
  • 12
    How about folders? I cant find a way to include folders and files inside by negating a rule. Commented Oct 30, 2013 at 16:24
  • 6
    About folders please check stackoverflow.com/questions/12799855/…
    – uzsolt
    Commented Jul 16, 2014 at 5:48
  • 2
    very nice. I am using this to have a repository in my home folder for things like vimrc and bashrc Commented Jun 18, 2015 at 11:53
  • 1
    @uzsolt does not work if you want to include one specific folder. see stackoverflow.com/questions/987142/…
    – DiCaprio
    Commented Dec 13, 2016 at 22:18
98

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

9
  • Do you also need .* for hidden files?
    – trusktr
    Commented Jun 4, 2013 at 5:42
  • 3
    The * is a wildcard even for ., so hidden files are also ignored, but then all hidden files ending in .c are included. Commented Apr 26, 2014 at 1:38
  • 2
    Using this method I think files from subfolders are also ignored. Check this for more details stackoverflow.com/a/11853075/739436
    – Stelian
    Commented May 4, 2015 at 7:38
  • 1
    I used this method in my home directory to only track my .bashrc and .bash_history e.g. * followed by !.bash* in the .gitignore file Commented Mar 17, 2016 at 16:09
  • 2
    You might need *.* on windows - see the answer by @Smaranjit Maiti Commented Jul 7, 2017 at 4:03
23

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

1
  • 4
    Yes I had the same problem with *, and had to use *.*. This is on Windows using gitbash and git version 2.9.0.windows.1 Commented Jul 7, 2017 at 4:01
18

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
9

I've seen a number of suggestions for the initial "ignore everything" rule, both here on SO and on other sites, but I found most of them to have thir own annoying usage issues. this has given rise to projects like the distributable .gitinclude.NET and the GH Pages hosted git-do-not-ignore, each of which simply help make this less tedious to manutain.

each of these (and many other blog posts) recommend starting out with a simply * to, quite literally, ignore all files and folders in the current root.

thereafter, including a file is "as simple" as prefixing the path with !, such as !.gitignore to ensure our repo doesn't ignore it's own .gitignore rules file.

the down side of this, is that when Git encounters an ignored folder, for performance reasons it does not check it's contents. trying to not ignore a file in a nested path gets very cumbersome:

# ...when ignoring all files and folders in the current root
*

!custom_path                       # allow Git to look inside this folder
custom_path/*                      # but ignore everything it contains
!custom_path/extras                # allow Git to look inside this folder
custom_path/extras/*               # but ignore everything it contains
!custom_path/extras/path_to_keep   # allow Git to see the file or folder you want to commit

so to offer an alternative idea, I've just configured a .gitignore file in the root of my Windows user profile folder, starting with **/* instead of the commonly seen * or *.*.

thereafter, each path that I want to explicitly include requires only one entry per tree level. slightly simplifying the previous example to the following:

# ...when ignoring all files recursively from the current root
**/*

!custom_path                       # allow Git to look inside this folder
!custom_path/extras                # allow Git to look inside this folder
!custom_path/extras/path_to_keep   # allow Git to see the file or folder you want to commit

this is not exactly a massive difference, but it is enough of a difference to make the file much easier to read and maintain, especially when trying to "un-ignore" a file nested about 5 levels deep...

1
  • The problem with this method is that you can't unignore directories, only individual files (unless I'm missing something) Commented Nov 23, 2023 at 10:29
1

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

!.*
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.