git 分支:用于发布/开发的不同配置文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9636492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Branching: different config files for release/development
提问by robertrv
I've inherited a project and we are using git. We have a number of environments (dev, test, prod). The previous team basically recreated everything on each instance, using the same accounts, passwords, sid, etc. The only thing that changed was the hostname mappings in /etc/hosts. So that it would connect to a different database server.
我继承了一个项目,我们正在使用 git。我们有许多环境(开发、测试、生产)。以前的团队基本上在每个实例上重新创建了所有内容,使用相同的帐户、密码、sid 等。唯一改变的是 /etc/hosts 中的主机名映射。这样它就可以连接到不同的数据库服务器。
Now, this creates a problem, because I can't, for example copy a schema so that a developer can run an experiment using the same database instance as the main development server. I basically have to create a new database instance on another host, and change /etc/hosts to point to that new server.
现在,这产生了一个问题,因为我不能,例如复制模式,以便开发人员可以使用与主开发服务器相同的数据库实例运行实验。我基本上必须在另一台主机上创建一个新的数据库实例,并将 /etc/hosts 更改为指向该新服务器。
While this is currently a working setup, I'm trying to find a way to maintain different config files for each instance. ie: Different versions of applicationConfig.xml
depending on the branch. I'm guessing one could argue that keeping database credential in the repo is not such a great idea, but lets just ignore that for a second.
虽然这是目前的工作设置,但我正在尝试找到一种方法来为每个实例维护不同的配置文件。即:applicationConfig.xml
取决于分支的不同版本。我猜有人可能会争辩说,将数据库凭据保存在 repo 中并不是一个好主意,但让我们暂时忽略这一点。
Another situation that might warrant having a different version of a file could be debugging. Say I'm using a javascript logger framework, and I add debug code that I wouldn't want to ship with a production release. I don't want to have to add the logger stuff when developing/testing and then remove it again before releasing. One might forget to do it.
另一种可能需要使用不同版本文件的情况可能是调试。假设我正在使用一个 javascript 记录器框架,并且我添加了我不想随生产版本一起提供的调试代码。我不想在开发/测试时添加记录器的东西,然后在发布之前再次删除它。人们可能会忘记这样做。
What's the proper way to deal with different "versions" of a file for different branches? Is there a way to have a branch that remains in sync with the latest code on master, but with a few config/code files changed? I don't expect it to remain in sync automatically, but I'd like to be able to not merge the config files (or parts of them), while not ignoring them completely (?). For example: don't merge lines 6,7 (db username and password), but do merge the other changes to the files.
处理不同分支的文件的不同“版本”的正确方法是什么?有没有办法让分支与 master 上的最新代码保持同步,但更改了一些配置/代码文件?我不希望它自动保持同步,但我希望能够不合并配置文件(或其中的一部分),同时又不完全忽略它们(?)。例如:不要合并第 6,7 行(db 用户名和密码),但要合并对文件的其他更改。
采纳答案by ralphtheninja
It sounds like you should read up on git attributes. Check out the section at the bottom of this page
听起来您应该阅读 git 属性。查看本页底部的部分
This is helpful if a branch in your project has diverged or is specialized, but you want to be able to merge changes back in from it, and you want to ignore certain files. Say you have a database settings file called database.xml that is different in two branches, and you want to merge in your other branch without messing up the database file.
如果您的项目中的某个分支出现分歧或专门化,但您希望能够从中合并更改,并且您希望忽略某些文件,这将很有帮助。假设您有一个名为 database.xml 的数据库设置文件,它在两个分支中不同,并且您希望在不弄乱数据库文件的情况下在另一个分支中进行合并。
回答by klaustopher
As you already said, storing the config settings inside your code isn't a good idea. Especially, your developers shouldn't even know the credentials for the production database, in my opinion.
正如您已经说过的,将配置设置存储在您的代码中并不是一个好主意。特别是,在我看来,您的开发人员甚至不应该知道生产数据库的凭据。
What we do here is that on each server we have predefined environment variables pointing to a configuration file that has the necessary credentials. Since we are using Java here, this is a file like database.properties
and this file is never inside a version control system.
我们在这里所做的是,在每台服务器上,我们都有预定义的环境变量,指向具有必要凭据的配置文件。因为我们在这里使用 Java,所以这是一个类似的文件database.properties
,这个文件永远不会在版本控制系统中。
This also makes it possible to have different settings and different credentials on each server
这也使得可以在每个服务器上拥有不同的设置和不同的凭据