Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make .gitignore ignore everything except a few files

Tags:

git

gitignore

I understand that a .gitignore file cloaks specified files from Git's version control. I have a project (LaTeX) that generates lots of extra files (.auth, .dvi, .pdf, logs, etc) as it runs, but I don't want those to be tracked.

I'm aware that I could (maybe should) make it so all those files are put in an separate subfolder in the project, since I could then just ignore the folder.

However, is there any feasible way to keep the output files in the root of the project tree and use .gitignore to ignore everything except the files I'm tracking with Git? Something like

# Ignore everything *  # But not these files... script.pl template.latex # etc... 
like image 424
Andrew Avatar asked Jun 12 '09 15:06

Andrew


People also ask

How do I Gitignore everything except?

You want to use /* instead of * or */ in most cases The above code would ignore all files except for . gitignore , README.md , folder/a/file. txt , folder/a/b1/ and folder/a/b2/ and everything contained in those last two folders.

How do you exclude or ignore some files from git commit?

Use an exclude file The exclude file lets Git know which untracked files to ignore and uses the same file search pattern syntax as a . gitignore file. Entries in an exclude file only apply to untracked files, and won't prevent Git from reporting changes to committed files that it already tracks.

Can Gitignore ignore folders?

gitignore is a plain text file in which each line contains a pattern for files or directories to ignore. It uses globbing patterns to match filenames with wildcard characters. If you have files or directories containing a wildcard pattern, you can use a single backslash ( \ ) to escape the character.


2 Answers

An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources.

# Ignore everything *  # But not these files... !.gitignore !script.pl !template.latex # etc...  # ...even if they are in subdirectories !*/  # if the files to be tracked are in subdirectories !*/a/b/file1.txt !*/a/b/c/* 
like image 189
Joakim Elofsson Avatar answered Sep 29 '22 07:09

Joakim Elofsson


If you want to ignore the whole content of a directory except one file inside it, you could write a pair of rules for each directory in the file path. E.g. .gitignore to ignore the pippo folder except from pippo/pluto/paperino.xml

pippo/* !pippo/pluto pippo/pluto/* !pippo/pluto/paperino.xml 

Note that if you simply had written above:

pippo/* !pippo/pluto/paperino.xml 

It wouldn't work because the intermediary pluto folder would not exist to Git, so paperino.xml could not find a place in which to exist.

like image 36
Giuseppe Galano Avatar answered Sep 29 '22 09:09

Giuseppe Galano