在Git分支中保存特定文件的不同内容 [英] Keep different content of a particular file in a Git branch

查看:478
本文介绍了在Git分支中保存特定文件的不同内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 config.php ,假设在不同分支的内容不同,例如。 测试 master

I have a config.php which suppose to be different in content in different branches, e.g. testing and master.

问题(防止将文件从master与Git合并)如何防止此文件合并。

I have asked in another question (Prevent merging a file from master with Git) that how to prevent this file from merging.

但我想知道,这是正确的方法吗?

But I am wondering, is this the correct way to do so?

我相信这是一个常见的用例,在不同的环境中有不同的配置文件,你想要跟踪配置,是吗?

I believe this is quite a common use case to have different config files in different environments and you want the config to keep tracked, right?

推荐答案

这样做的经典方法是有一个名为 config.yml-dist 的默认配置文件(让我们假设你的原始文件被称为 config.yml );您可以在.gitignore中添加原始文件,并只在 dist 中添加原始文件。

部署应用程序或重新克隆项目后,只需 cp config.yml-dist config.yml ,并更改所需的设置。

The classic way to do this is to have a default config file called config.yml-dist (let's pretend that your original file is called config.yml) ; you add the original file in .gitignore, and version only the dist one.
After you deploy your app or re-clone the project, simply cp config.yml-dist config.yml, and change the settings you want.

此方法由我在PHP 行业中遇见的许多人使用。

This method is used by many people I met in the PHP industry.

但是,有一个我喜欢更多,我发现更清洁:使用环境变量。
示例:

But, there's one I like much more and that I find cleaner: using environment variables. Example:

username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>

这样,您将拥有一个单一版本配置文件,一。

This way, you'll have one single versioned configuration file and won't have to edit a single one.

这篇关于在Git分支中保存特定文件的不同内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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