Question

How do I make mercurial ignore any file with .xxx extension

I want Mercurial to ignore any file with a certain extension.

For example, I wanted to ignore files with a .SUO extension. (There's no need to version-control Visual Studio user settings.)

So I changed my .hgignore file to this:

syntax: glob
*.suo

However, this has no effect, and Mercurial still sees my .suo file.

What am I doing wrong here?

 21  16798  21
1 Jan 1970

Solution

 27

If, when running hg status before altering your .hgignore file, the .suo file had a ? in front of it, then it should be ignored now. If anything else (M or A for example) it is already tracked by the repository and will not magically stop being tracked. In such a case you'll need to do hg remove on the file to delete it and have hg stop tracking it, or just do hg forget on it to have hg stop tracking it but keep the file. Either should be followed by a commit.

The only files that will be omitted from the status listing if their path matches a pattern in the .hgignore file are files that are not tracked. It would make no sense to omit a file that is tracked, because you would never see whether it had been modified, added, or removed.

Edit: Mercurial does only track files (you can't make it track empty directories), but the patterns in .hgignore are simply run against strings of the file paths relative to the root of the repository. The very same relative paths that it shows you when you run hg status. So it does work how you say you want it to work because the following lines are a standard part of my own .hgignore files:

syntax: glob
*\obj\*
*\bin\*
*.csproj.user
*.suo

Again, when you run hg status and it shows a .suo file, what single character is at the beginning of that line? Is it a M, A, R, ! or ? character? What is the path after it?

2011-05-20

Solution

 8

Mercurial uses entries in a file called .hgignore to determine what files it completely ignores. It is normally located in the root file for your repository (and not in the .hg directory, which you might think).

You can find out more here:

http://www.selenic.com/mercurial/hgignore.5.html

Normally, we use regular expression syntax to ensure that case is not a factor in extensions:

# use regexp syntax.
syntax: regexp
(?i)\.dcu
(?i)\.identcache
(?i)\.dof
(?i)\.dsk
(?i)\.bak
(?i)\.old

That way, it ensures that even if for some reason the case of the extension changes, it is still ignored.

2011-05-20