Git:如何忽略对跟踪文件的更改? [英] Git: How to ignore changes to a tracked file?

查看:113
本文介绍了Git:如何忽略对跟踪文件的更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个带有数据库设置的文件,我将其设置为一些默认值。该文件由 Git 跟踪并检入。由于此文件将使用各种开发人员机器的不同值进行编辑,因此我可以告诉 Git 忽略对此文件的新更改?

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

我尝试将文件添加到 .gitignore 文件,但由于已跟踪文件,因此不会忽略它。这在其他情况下也很好,但是我想知道是否可以在这里做些什么?

I tried adding the file to the .gitignore 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?

推荐答案

我建议命名源代码控制文件与实际预期名称不同。例如,如果文件通常命名为 config.json ,则将示例文件命名为 config.json.dist 并提交此文件。然后将 config.json 添加到您的 .gitignore 文件中。克隆后,您的开发人员只需 cp config.json.dist config.json ,然后根据需要对其进行编辑,即可进行后续提交,而不必担心意外更改默认文件或忘记忘记一直打开和关闭某些设置。

I recommend naming the source-controlled file differently than its actual expected name. For example, if the file is normally named config.json, then name your example file config.json.dist and commit this file. Then add config.json to your .gitignore file. Your devs would simply cp config.json.dist config.json after cloning, and then edit it as required, making subsequent commits without having to worry about accidentally changing the default file or forgetting to toggle some setting on and off all the time.

您甚至可以编辑代码以搜索 config.json 首先,如果不存在,则退回到 config.json.dist 。这将使开发人员甚至无需执行复制步骤即可工作。 (这就是PHPUnit的工作方式。)

You might even edit your code to search for config.json first, and if that doesn't exist, fall back to config.json.dist. This would allow the devs to work without even performing the copy step. (This is how PHPUnit works.)

这篇关于Git:如何忽略对跟踪文件的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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