Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git ignore filenames which contain <pattern to ignore>

Tags:

I am trying to tell git to ignore files which have _autosave somewhere in the filename. An example of such a file is:

hats/TFCB_ATV_TOP_HAT/_autosave-TFCB_ATV_HAT.kicad_pcb

In a regular expression, I would simply use the pattern ^.*_autosave.*

Of course, git doesn't use regular expressions to evaluate its .gitignore file. I did some reading and had the impression that *_autosave* would work, but it doesn't.

What is the correct syntax to tell git to ignore these files?

like image 209
Brian J Hoskins Avatar asked Sep 01 '15 15:09

Brian J Hoskins


People also ask

How do I ignore a file name 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 text file in Gitignore?

You have the option to use double Asterisk (**) to match any number of directories and files. For example, Test/**/*. txt will tell git to ignore only files ending with . txt in the test directory and its subdirectories.

How do I force git to ignore a file?

Use Git update-index to ignore changes Or, you can temporarily stop tracking a file and have Git ignore changes to the file by using the git update-index command with the assume-unchanged flag.

Which files should I ignore in git?

gitignore should contain all files that you want to ignore. Therefore, you should ignore files generated by the OS, the IDE you are working on... My question appears when the repository is on Github and people can clone it and push the changes. These people can use other operating systems and can use other IDEs.


1 Answers

Add this line to your .gitignore

*_autosave* 

According to git help gitignore

patterns match relative to the location of the .gitignore file

Patternz like *_autosave* match files or directories containing "_autosave" somewhere in the name.

Two consecutive asterisks ("**") in patterns mathed against full pathname may have special meaning

A leading "**" followed by a slash means match in all directories.

But "**/" seams redundant in some enviroments.

EDIT:

My machine at work (using git 1.7.1) does not have support for dubbel asterisk, but with *_autosave* it excludes the files.

here is a simple test scrtipt (for Linux)

DIR="$(mktemp -d)" git init $DIR/project1 cd $DIR/project1 cat > .gitignore <<EOF **/*_autosave* *_autosave* EOF mkdir dir1 touch README foo_autosave dir1/bar_autosave git status  rm -rf $DIR/project1 
like image 96
joran Avatar answered Oct 21 '22 16:10

joran