告诉git永远不要更新文件 [英] Tell git never to update a file

查看:614
本文介绍了告诉git永远不要更新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在git中有几个文件(即配置文件),我需要在git repo中,但我不希望它们更新(出于某种原因,运行它们并进行配置,更改配置文件)。

那么有什么办法可以告诉git忽略对文件的任何修改,但要保持原始文件仍在repo中?目前我发现这样做的唯一方法是将文件添加到.gitignore文件,并且git直接将文件添加到项目中(使用-f覆盖)。有没有更好的方法?

解决方案

我不会用git来管理这些文件。我会把它们放在你的 .gitignore 中,这样git会忽略它们,并把模板文件(比如 foo.template 为一个名为 foo ,或者 template / foo )的文件。然后,当你克隆repo时,你可以拷贝它们并修改它们,git不会对拷贝做任何事情,同时仍然管理模板。如果你有几个这样的文件,你可以提供一个脚本或制作规则或类似的东西来从你的模板中填充你的所有配置文件,以便更容易设置。



更好的设计是将项目配置从本地配置中分离出来,或者将项目级别的默认值设置为可以在单独的本地文件中重写。项目级文件被提交,本地文件被忽略,并且它们都被使用。也许一些配置设置只对项目有意义,有些配置设置只在本地有意义,在这种情况下,您只需从适当的文件中提取适当的设置;或者可以使用项目设置作为默认值,并让本地配置覆盖默认值。这样,您可以同时更新每个人共享的项目设置,同时不会干扰人员的本地设置。当然,这取决于对设置格式的控制;如果您使用的第三方工具以特定格式进行设置,所有这些都会混合到一个文件中,您可能必须使用模板方法。


I have a few files in git (namely configure files), that I need to be in the git repo, but I don't want them to ever update (for some reason, running them, and make, changes the configure file).

So is there any way I can tell git to ignore any CHANGES to the file, but to keep the original file still in the repo? Currently the only way I've found out to do something like this is to add the file to the .gitignore file, and the git add the file to the project directly (using -f to override). Is there any better way?

解决方案

I wouldn't manage those files with git at all. I'd put them in your .gitignore so git would ignore them, and put template files (such as foo.template for a file named foo, or maybe template/foo) into git. Then when you clone the repo, you can copy them out, and modify them, and git won't do anything with the copies, while still managing the templates. If you have several such files, you can supply a script or make rule or something of the sort to populate all of your config files from their templates, to make it easier to get set up.

A better design would be to separate project config from local config, or have project level defaults that can be overridden in a separate local file. The project level files get committed, the local files get ignored, and they are both used. Perhaps some config settings only make sense for the project and some only make sense locally, in which case you just pull the appropriate setting from the appropriate file; or you could use the project setting as defaults, and let the local config override the defaults. This way, you can update the project settings, which are shared, for everyone at once, while not interfering with people's local settings. Of course, this depends on having control of the settings format; if you're using a third-party tool that takes its settings in a particular format, all mixed into one file, you'll probably have to use the template approach.

这篇关于告诉git永远不要更新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆