How to tell git to ignore a specific file if and only if another file exists?

I would like to tell git ignore a file called foo.ext if and only if there is another file called foo.txe . I'm pretty sure this can't be done with the .gitignore syntax, but there can be other complicated ways that I don't think about?

Usage example: I often store LaTeX document data in a subdirectory named figures . And I usually draw my figures with Xfig. My makefile calls fig2dev to create a pair of files from the figures/foo.fig :

  • figures/foo.pdf , a PDF file for the graphic part,
  • figures/foo.pdf_t , LaTeX file for text, with includegraphics command to include PDF.

Since these two files are generated, I would like git ignore them. I could add two simple exception patterns to my .gitignore file, but it also happens that some of my numbers are in a regular PDF file, not generated from another source. And of course, I would like git not to ignore these figures/bar.pdf .

I would like to add something like:

 /\(**\)/figures/\(*\).fig: /\1/figures/\2.pdf /\1/figures/\2.pdf_t 

to my .gitignore file, which means that any file named /<some_path>/figures/<some_name>.pdf or /<some_path>/figures/<some_name>.pdf_t should be ignored if and only if a file exists with named /<some_path>/figures/<some_name>.fig with the same <some_path> and <some_name> .

+7
git gitignore
source share
1 answer

You will need to use something in the file or folder name to determine which files are automatically generated and which are not. Depending on how your code is configured, you can ignore it based on the folder. For example, use figure-auto for an automatically generated file and figure for a manually generated file.

figure-auto/

If you have only a few files that you do not want to ignore, you can use

 figures/*.pdf figures/*.pdf_t !figures/keep-in-source-control.pdf !figures/manually-created.pdf 

Git Ignore Pattern Format

+2
source share

All Articles