0

I've got some temp files that are named:

#myfile#
.#anotherfile
./some/dir/#myfile#
./somewhere/.#anotherfile

I tried adding the (to me) obvious pattern, see command below

cat >> .gitignore <<EOF
#.*
.*#.*
EOF

but git status still lists the files.

I realized that the hash '#' symbol is the comment character, so I tried quoting it, and added:

\#.*
.*\#.*

But that didn't work either.

Searches for it didn't give me an answer, but did make me realize that git doesn't use normal regular expression syntax so my guesses were not valid for git.

Some good searches were

My .gitignore now has:

\#*#
.\#*#

I now just have the one pattern that needs fixing:

./somewhere/.#anotherfile
PatS
  • 4,099
  • 6
  • 32
  • 52

2 Answers2

0

The right glob is *#* since * matches zero or more of any character (within a name component).

You would need a backslash if and only if the # were the first character. Since it isn't, you don't. ("If it was so, it might be; if it were so, it would be; but as it isn't, it ain't.") That is, if you wanted to ignore files like #ignoreme and #thistoo, but not ignore commit#me, you would want #*, and since #* begins with # you would have to write this as \#*.

(Aside: Git's ignore pattern matcher deliberately disobeys the star does not match leading dot behavior, i.e., *foo matches .foo even though sh/bash *foo does not match .foo. That is, Git uses the POSIX fnmatch rules as if FNM_PERIOD is not set.)

torek
  • 330,127
  • 43
  • 437
  • 552
0

@torek, thanks for your answer. I tried just *#* but that didn't work.

Through experimentation, I found the following to work:

**.\#*   => handles .#tempfile and sub/dirs/.#tempfile, .#tempfile#, sub/dir/.#tempfile#
**\#*   => handles #tempfile, sub/dirs/#tempfile, and #tempfile#, sub/dir/#tempfile#

I've annotated the pattern to show the files that it matches but obviously the => text would not be in the .gitignore file. For me this is fine so this solves my question.

To test, I used the following .gitignore.

**.\#*
**\#*

With these files:

touch '.#file'
touch '.#file#'
touch '#file'
touch '#file#'
touch 'commit#file'
touch 'bin/.#file'
touch 'bin/.#file#'
touch 'bin/#file'
touch 'bin/#file#'
touch 'bin/commit#file'

All of the files were filtered out when I tested this.
NOTE: The file commit#file and bin/commit#file were also filtered out. I wanted this, but others might not.

Tested with git-bash (v 2.17.1 on windows) and git (1.8.x.x) on Linux.

I've created a small test repo on github to show this. See https://github.com/steranka/git-ignore-test

PatS
  • 4,099
  • 6
  • 32
  • 52
  • NOTE: When using Windows git-bash, the files show up with single tick quotes around some of them when running the ls command. When examining the temp files from windows explorer, the quotes do not appear, so while this is strange it was observed. – PatS Sep 19 '18 at 16:47
  • 1
    Bash itself uses `#` as comment, so when bash (or any other program) is producing strings meant for bash to consume later, it needs to do its own quoting. Single quotes work well for this, so they are pretty common here. – torek Sep 19 '18 at 20:40