191

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?

Jakub Narębski
  • 289,171
  • 61
  • 216
  • 230
Daisy Sophia Hollman
  • 5,488
  • 6
  • 22
  • 32

6 Answers6

275

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

T.E.D.
  • 42,800
  • 9
  • 66
  • 134
  • 23
    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. – Daisy Sophia Hollman Aug 14 '09 at 19:44
  • 10
    How about folders? I cant find a way to include folders and files inside by negating a rule. – Marcio Cruz Oct 30 '13 at 16:24
  • 4
    About folders please check http://stackoverflow.com/questions/12799855/configure-git-to-track-only-one-file-extension – uzsolt Jul 16 '14 at 05:48
  • 2
    very nice. I am using this to have a repository in my home folder for things like vimrc and bashrc – Martin Capodici Jun 18 '15 at 11:53
  • 1
    @uzsolt does not work if you want to include one specific folder. see http://stackoverflow.com/questions/987142/make-gitignore-ignore-everything-except-a-few-files/29932318#29932318 – DiCaprio Dec 13 '16 at 22:18
  • @dyslexicanaboko - Well, a decade ago when I wrote this, "RTFM" was a pretty typical response online to questions with a reference answer, and the phrase was so ubiquitous that between techies it wasn't even considered particularly rude. People often applied it to themselves (as I did here). These days I don't think I'd necessarily throw that in there... – T.E.D. Jun 17 '18 at 00:12
  • I don't think it hurts sometimes. I run my own blog and the only time I create an article is if I can't find the answer or it wasn't obvious. Usually RTFM or TFM applies - people like to ask before they look it up. Either way I like your reply. – dyslexicanaboko Jun 24 '18 at 01:34
  • It doesn't seem to work with folders. I'm trying to include a specific path to a file, after its upper-level folder has been ignored, that doesn't seem to work. – Shimmy Weitzhandler Jul 19 '20 at 03:41
  • @ShimmyWeitzhandler - Try reading the comments about 5 above yours. – T.E.D. Jul 20 '20 at 12:42
  • After restarting VS things are working now. It was something inside the `.vs` folder, so it's understandable. Thank you. – Shimmy Weitzhandler Jul 21 '20 at 00:46
  • For my case only `!**/my-folder/build` worked. Remember to reapply `.gitignore` as needed. – Jacksonkr Jul 06 '21 at 16:56
87

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

Vineel Kumar Reddy
  • 4,330
  • 9
  • 32
  • 36
  • Do you also need `.*` for hidden files? – trusktr Jun 04 '13 at 05:42
  • 2
    The `*` is a wildcard even for `.`, so hidden files are also ignored, but then all hidden files ending in `.c` are included. – Rohmer Apr 26 '14 at 01:38
  • 2
    Using this method I think files from subfolders are also ignored. Check this for more details http://stackoverflow.com/a/11853075/739436 – Stelian May 04 '15 at 07: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 – user5359531 Mar 17 '16 at 16:09
  • This is exactly what I was looking for! – Ulf Aslak Dec 09 '16 at 13:44
  • 2
    You might need `*.*` on windows - see the answer by @Smaranjit Maiti – Martin Capodici Jul 07 '17 at 04:03
  • agree with @popas. "man gitignore" states that "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." – alpha_989 Dec 09 '17 at 20:11
  • if you have *.c in subdirectories, havin ga * as the first directory, effectively ignores allsubdirectories. So even you include a !*.c after *, the files in the subdirectories wont be included. – alpha_989 Dec 09 '17 at 20:12
  • yep, `*` does not work with my Git version on Windows...only `*.*` works – Csa77 Mar 06 '19 at 17:18
17

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

Smaranjit
  • 615
  • 6
  • 16
8

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
gzfrancisco
  • 762
  • 9
  • 13
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.

Christian Severin
  • 1,681
  • 20
  • 33
0

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

!.*
copeland3300
  • 561
  • 7
  • 11