Make git ignore generated files

git

Solution

Several simple solutions:

- Add all suffixes (e.g. `.pdf`) to `.gitignore` as `*.pdf`, and `git add -f` those `version-this-one.pdf` that should be versioned, `git` will keep track of those (need a bit of care). The `*.pdf` is really a glob, so you could exclude, say, PDFs whose hames start with uppercase by `[A-Z]*.pdf`.

- As above, but add e.g. `*.pdf` for all PDFs, and `!version-this-one.pdf` to exclude one from the excluded set (can also use a pattern here)

- Make a complete list of the generated files into `.gitignore`. Need to keep it up to date (not too bad, if a new one shows up, `git` will consider it untracked, just add it then).

- Mix and match: Use the above methods as you want for different filename patterns

- Per directory `.gitignore`: `git` searches first in this directory's `.gitignore`, if not found in the parent's, ... As you can exclude patterns with `!`, this stops a search there.

Check out `gitignore(5)`.

Problem

I have several projects that have a large number of files that, when processed, generate other files. There is no need to track the generated files, in fact, in some cases you do not want to track them as it will be a waste of space. I understand that that is something the `.gitignore` file is for. The problem is, I cannot find a good way to use pattern matching on the files, because the reps also contain regular user editted files that conform to the same patterns. For example, a repo will have a number of `.tex` files that are user editted, but also a number of `.tex` files that are produced by knitr form `.Rnw` files. Most `.pdf` files are auto generated from `.tex` files, but some are user created `.pdf` figures that will be included in TeX documents, and should be tracked. What would be a good way to make git for example ignore a file `blah.tex` only if there is `blah.Rnw` in the same directory, and to ignore `bluh.pdf` only if there is `bluh.tex` or `bluh.Rnw` in the same directory?

Original source