Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

gitignore

I'd like to tell git to ignore a file named foo.ext if and only if there exists another file named foo.txe. I am almost sure that this cannot be done with the .gitignore syntax but there are maybe other tricky ways that I do not think of?

Example of use: I frequently store the figures of a LaTeX document in a sub-directory named figures. And I usually design my figures with Xfig. My makefile calls fig2dev to generate a pair of files from the figures/foo.fig source:

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

Because these two files are generated I'd like git to ignore them. I could add two simple exclude patterns to my .gitignore file but it also happens that some of my figures are in plain PDF, not generated from another source. And of course, I'd like git not to ignore these figures/bar.pdf files.

I'd like to add something like:

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

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

like image 725
Renaud Pacalet Avatar asked Sep 16 '15 13:09

Renaud Pacalet


People also ask

How do I ignore a specific file in git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I ignore a specific file?

If you want to ignore only one specific file, you need to provide the full path to the file from the root of the project. If you want to ignore all files with a specific name, you need to write the literal name of the file. In this case, you don't need to provide the full path to a specific file.

How can I ignore particular file while doing 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 I have 2 git ignore?

gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple . gitignore files.


1 Answers

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

figure-auto/

If you only have a few files that you don't want to ignore you could use

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

Git Ignore Pattern Format

like image 130
Shawn D Avatar answered Oct 21 '22 17:10

Shawn D