Search code examples
gitgitignore

git ignore filenames which contain <pattern to ignore>


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?


Solution

  • 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