Mercurial: How to ignore changes to a tracked file

2019-01-06 13:04发布

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?

8条回答
SAY GOODBYE
2楼-- · 2019-01-06 13:42

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)
查看更多
一夜七次
3楼-- · 2019-01-06 13:42

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.

查看更多
迷人小祖宗
4楼-- · 2019-01-06 13:53

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
查看更多
该账号已被封号
5楼-- · 2019-01-06 13:53

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.

查看更多
爷、活的狠高调
6楼-- · 2019-01-06 13:55

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.

查看更多
放荡不羁爱自由
7楼-- · 2019-01-06 13:59

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

查看更多
登录 后发表回答