Git配置文件等最佳实践 [英] Git best practice for config files etc

查看:106
本文介绍了Git配置文件等最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对所有的东西都不熟悉,并且想知道什么是配置文件的最佳实践。我的本地开发服务器需要不同的配置值给我的实时服务器,所以我怎样才能阻止它推/拉这些文件? 使用解决方案

符号链接。

以一个名为config.ini的配置文件为例。在您的git仓库的工作目录中,您可以执行以下操作:


  1. 创建一个名为config -sample.ini。这是您完成所有工作的文件。

  2. 在config.ini和config-sample.ini之间创建一个符号链接。

      ln -s config-sample.ini config.ini 

    即使您真的维护config-sample.ini,这让我们所有的代码指向config.ini。


  3. 更新您的.gitignore以防止存储config.ini。也就是说,添加一个config.ini这一行:

      echoconfig.ini>> .gitignore 


  4. (可选,但强烈推荐)使用 config.ini export-ignore。

      echoconfig.ini export-ignore>> .gitattributes 


  5. 做一些编码和部署....


  6. 将代码部署到生产环境后,将config-sample.ini文件复制到config.ini。您需要进行必要的调整以设置生产。您只需在第一次部署时以及任何时候更改配置文件的结构时执行此操作。


这样做的好处:


  • 配置文件的结构保存在回购站中。


  • 对于dev和production之间相同的任何配置选项,可以维护合理的默认值。 当您将新版本推送到生产时,您的config-sample.ini将会更新。这使得更容易找到你需要在你的config.ini文件中进行的任何更改。

  • 你永远不会覆盖生产版本的config.ini。 (使用.gitattributes文件的可选步骤4增加了一个额外的保证,即使您不小心将它添加到回购站中,也不会导出config.ini文件。)


(这在Mac和Linux上非常适合我,我猜测Windows上有相应的解决方案,但其他人则需要对此进行评论。)

I'm still new to all things git and was wondering what is best practice in regards to config files. My local development server needs different config values to my live one so how can I stop it pushing / pulling those files?

解决方案

Use symbolic links.

Take an example where you have a config file named "config.ini". In the working directory of your git repo, you would do the following:

  1. Create a version of the config file called "config-sample.ini". This is the file you'll do all your work on.

  2. Create a symbolic link between "config.ini" and "config-sample.ini".

    ln -s config-sample.ini config.ini
    

    This let's all your code point to "config.ini" even though you're really maintaining "config-sample.ini".

  3. Update your .gitignore to prevent the "config.ini" from being stored. That is, add a "config.ini" line:

    echo "config.ini" >> .gitignore
    

  4. (Optional, but highly recommended) Create a .gitattributes file with the line "config.ini export-ignore".

    echo "config.ini export-ignore" >> .gitattributes
    

  5. Do some coding and deploy....

  6. After deploying your code to production, copy the "config-sample.ini" file over to "config.ini". You'll need to make any adjustments necessary to setup for production. You'll only need to do this the first time you deploy and any time you change the structure of your config file.

A few benefits of this:

  • The structure of your config file is maintained in the repo.

  • Reasonable defaults can be maintained for any config options that are the same between dev and production.

  • Your "config-sample.ini" will update whenever you push a new version to production. This makes it a lot easier to spot any changes you need to make in your "config.ini" file.

  • You will never overwrite the production version of "config.ini". (The optional step 4 with the .gitattributes file adds an extra guarantee that you'll never export your "config.ini" file even if you accidentally add it to the repo.)

(This works great for me on Mac and Linux. I'm guessing there is a corresponding solution possible on Windows, but someone else will have to comment on that.)

这篇关于Git配置文件等最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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