Mercurial: How to ignore changes to a tracked file

I have a file with database settings in my project which I have set to some defaults. The file is tracked by Mercurial and checked in. Since this file will be edited with different values various developer machines, is there a way I can tell Mercurial to ignore new changes to this file?

I tried adding the file to the .hgignore file, but since the file is tracked it isn't ignored. This is alright and good in other situations, but I am wondering if there is something I can do here?


Solution 1:

Using a file template is definitely the best solution. For example, if you have a database.ini file, commit a database.ini.template file and ignore database.ini in .hgignore

Solution 2:

If you always want to ignore the file, you can add the -X option as a default for commit to your .hg/hgrc configuration file:

[defaults]
commit = -X program.conf

Solution 3:

We wrote an extension for this called exclude. It will automatically add the -X options on the commands that support them -- so hg status and hg commit wont see the modified file. It works by reading a .hgexclude file from the root of your repository, just like the .hgignore file. You add the files that you want to exclude there:

syntax: glob
db.conf

The extension works quite well, but there is a known situation where it fails: merges and the commit that follows a merge (this is documented on the wiki). It would need to be improved so that it would save the modifications away to a temporary file and then restore them afterwards. Please get in contact if you need this feature.

Solution 4:

There is no truly automated process, but you can try (as in this SO question) the -X option on hg commit:

% hg stat
M myfile
% hg commit -X 'myfile'

(other solutions might involve shelve or hq)

However, this is not the "right" solution. I would rather recommend versioning:

  • a file template
  • a script able to generate the final file (that you modify but can ignore altogether)