Mercurial: How to ignore changes to a tracked file

2019-01-06 13:36发布

问题:

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?

回答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



回答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


回答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.



回答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)


回答5:

If you are using TortoiseHG, open the Settings for the repo, go to the Commit section (2nd icon down on the left) & add the file name(s) to the Auto Exclude list on the right (~ 3rd from the bottom in the list).

From https://tortoisehg.readthedocs.io/en/latest/settings.html#commit



回答6:

Typically you would check in a reference copy of the file and track it then have the developers make a copy of that for local development, you wouldn't really want developers editing the source controlled file for their own environments.

If your configuration system supports it, it's even easier if you can use an override file that simply override values in the reference copy (e.g. the database connection string). That way devs only have to keep a very minimal local set of override values.



回答7:

You can try hg forget. For more details, see the official manual about the same command. It worked for me.

I think, something like this is closer to a correct answer to the original question Mercurial: How to ignore changes to a tracked file, rather than the others suggesting a template, etc.



回答8:

If the file is already being tracked, you can issue the Forget command to the file. If you're using TortoiseHg just right click the file during commit and select Forget. The file must also be already in the ignore list.

I had the same problem as yours, I file keeps on appearing on every commit even-though its already in the ignore list. I tried the Forget command and it did the trick.