34

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?

dejanualex
  • 3,872
  • 6
  • 22
  • 37
Brian J Hoskins
  • 509
  • 1
  • 4
  • 8

1 Answers1

67

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
Community
  • 1
  • 1
joran
  • 2,815
  • 16
  • 18
  • Your answer didn't work for me: git is still trying to track the files. I will have to do some more reading. This should be simple!! – Brian J Hoskins Sep 02 '15 at 07:12
  • 1
    @BrianJHoskins, gitignore has no affect on files that are already tracked by git, can you add the output from `git status` to your question? – joran Sep 02 '15 at 07:18
  • Thanks joran; after I posted my comment I had a feeling it would invite a response about currently tracked files - I should have made it clearer. Git is showing the files as untracked, but it isn't ignoring them using the pattern above. – Brian J Hoskins Sep 02 '15 at 07:22
  • 1
    I retract my statement that it doesn't work. It DOES! I am not sure what happened there. [asterix]_autosave[asterix] works fine. Your answer explains this but the top few lines indicate the wrong thing to put inside the .gitignore file. If you change it to the solution you put further down in your answer, I can accept it as correct. – Brian J Hoskins Sep 02 '15 at 07:36