154

I am looking for an expression for the .hgignore file, to ignore all files beneath a specified folder.

eg: I would like to ignore all files and folders beneath bin

Actually any advice on how the expressions are formed would be great

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
Xian
  • 76,121
  • 12
  • 43
  • 49

6 Answers6

178

Alternately:

syntax: glob
bin/**
Ry4an Brase
  • 78,112
  • 7
  • 148
  • 169
  • this is much easier I think :) – Xian Nov 02 '08 at 10:03
  • PhiLho, I think that would also filter out files named bin – Peter Gibson May 16 '11 at 23:13
  • 1
    This answer, plus this documentation helped me: http://www.selenic.com/mercurial/hgignore.5.html – CenterOrbit Oct 04 '12 at 04:31
  • 9
    `bin` `bin/` `bin/*` `bin/**` - All do the same thing with glob i.e. ignore all directories & sub-directories named bin but not any file name bin.txt or bin1 . What's the point of asterix? – user Jun 30 '13 at 13:03
  • I like it as clarity, but certainly use 'bin' if you've internalized the glob rules (I'm a `re` sort of guy) – Ry4an Brase Jul 01 '13 at 05:19
  • I have had serious problems with ignoring `bin`, it seams to be special. I can have the correct pattern and it does not work, I then use another pattern (`Debug` the only subdirectory) and the directory is ignored, then switch back to the correct one and all is ok. I have no idea where this memory is coming from. – ctrl-alt-delor Jan 29 '14 at 12:27
  • I think that @buffer is wrong. For experience I have sow that when I use bin/ or bin/* only ignore one level and with bin/** it ignore things in bin and subfolders. But I think this is a bug or wrong interpretation of spec. – PhoneixS Mar 27 '14 at 11:11
  • If you use TortoiseHg, here's a link about ignoring files that may be helpful. https://tortoisehg.readthedocs.io/en/latest/ignore.html – Doug J. Huras Feb 08 '21 at 14:34
55

I did some experiments and I found that the regex syntax on Windows applies to the path starting with the current repository, with backslashes transformed to slashes.

So if your repository is in E:\Dev for example, hg status will apply the patterns against foo/bar/file1.c and such. Anchors apply to this path.

So:

  • Glob applies to path elements and is rooted to element parts
  • foo matches any folder (or file) named foo (not to "foobar" nor "barfoo")
  • *foo* matches any folder or file with "foo" in the name
  • foo/bar* matches all files in "foo" folder starting with "bar"


  • Regex is case sensitive, not anchored
  • Of course, backslash regex special characters like . (dot)
  • / matches \ path separator on Windows. \ doesn't match this separator...
  • foo matches all files and folders with "foo" inside
  • foo/ matches only folders ending with "foo"
  • /foo/ matches the folder "foo" somewhere in the path
  • /foo/bar/ matches the folder "bar" in the folder "foo" somewhere in the path
  • ^foo matches file or folder starting by foo at the root of the repository
  • foo$ matches file ending with foo

I hope this will help, I found the HGIGNORE(5) page a bit succinct.

PhiLho
  • 40,535
  • 6
  • 96
  • 134
10

Both of those will also filter out a directory called cabin, which might not be what you want. If you're filtering top-level, you can use:

^/bin/

For bin directories below your root, you can omit the ^. There is no need to specify syntax, regexp is the default.

Derek Slager
  • 13,619
  • 3
  • 34
  • 34
  • 4
    This won't ignore anything. The caret says only things in the repo root but the leading slash catches only dirs not in the repo root. – Ry4an Brase Nov 24 '08 at 04:14
3

syntax: glob bin/**

This answer is shown above, however I'd also like to add that * and ** are handled differently. ** is recursive, * is not.

See Hg Patterns

balrob
  • 585
  • 6
  • 7
2

Nevermind, I got it

syntax: regexp
bin\\*

expressions follow standard perl regular expression syntax.

Xian
  • 76,121
  • 12
  • 43
  • 49
  • 2
    Indeed, and your expression means "file or folder ending with bin and followed by 0 or more backslashes"... So it works because the backslash is nullified by the *, but it applies anything with "bin" inside. On Windows, hg replaces \ by / before matching. – PhiLho Dec 06 '08 at 12:32
-2

to ignore .class files

syntax: regexp
?\.class
user2427
  • 7,842
  • 19
  • 61
  • 71