配置文件等的 Git 最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9342886/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:30:51  来源:igfitidea点击:

Git best practice for config files etc

gitgithubconfig

提问by Martin Hunt

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?

我对 git 的所有事情还是陌生的,想知道关于配置文件的最佳实践是什么。我的本地开发服务器需要与我的实时服务器不同的配置值,那么我该如何阻止它推/拉这些文件?

回答by Alan W. Smith

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:

以您有一个名为“config.ini”的配置文件为例。在 git repo 的工作目录中,您将执行以下操作:

  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.

  1. 创建一个名为“config-sample.ini”的配置文件版本。这是您将完成所有工作的文件。

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

    ln -s config-sample.ini config.ini
    

    这让您的所有代码都指向“config.ini”,即使您确实在维护“config-sample.ini”。

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

    echo "config.ini" >> .gitignore
    
  4. (可选,但强烈推荐)使用“config.ini export-ignore”行创建一个 .gitattributes 文件。

    echo "config.ini export-ignore" >> .gitattributes
    
  5. 做一些编码和部署....

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

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.)

  • 配置文件的结构在 repo 中维护。

  • 可以为开发和生产之间相同的任何配置选项维护合理的默认值。

  • 每当您将新版本推送到生产时,您的“config-sample.ini”都会更新。这使得更容易发现您需要在“config.ini”文件中进行的任何更改。

  • 您永远不会覆盖“config.ini”的生产版本。(带有 .gitattributes 文件的可选步骤 4 增加了额外的保证,即使您不小心将其添加到存储库中,您也永远不会导出“config.ini”文件。)

(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.)

(这对我来说在 Mac 和 Linux 上非常有用。我猜在 Windows 上可能有相应的解决方案,但其他人将不得不对此发表评论。)

回答by joeytwiddle

Various options are available:

有多种选择:

1. Commit a default config file, but allow a local config file

1. 提交默认配置文件,但允许本地配置文件

Add a file default.confto your Git repository.

将文件添加default.conf到您的 Git 存储库。

Your app first looks for app.confbut if that does not exist, it uses default.conf.

您的应用程序首先查找,app.conf但如果不存在,则使用default.conf.

Users who want the non-default config can copy default.confto app.confand then edit it.

想要非默认配置的用户可以复制default.confapp.conf然后编辑它。

Users should not commit app.confinto the repository, because different users may want different settings in that file. (So you should put app.confinto your .gitignore.)

用户不应提交app.conf到存储库,因为不同的用户可能希望该文件中的设置不同。(所以你应该放入app.conf你的.gitignore.)

With a twist (recommended)

扭曲(推荐)

Your app always loads default.confbut if app.confis present then it will copysettings from app.confover the settings from default.conf.

你的应用始终负荷default.conf,但如果app.conf存在,则它会复制从设置app.conf在从设置default.conf

This twist has a couple of advantages:

这种扭曲有几个优点:

  • app.confonly needs to hold the differences from the defaults, making it smaller and easier to maintain.

  • When the app changes, new defaults added to default.confwill be available to the app, without the user having to copy them into app.conf.

  • app.conf只需要保留与默认值的差异,使其更小且更易于维护。

  • 当应用程序更改时,添加到的新默认值default.conf将可供应用程序使用,而无需用户将它们复制到app.conf.

This solution is pretty similar to Alan W. Smith's answer above, with a small difference: If the app can start without the app.conffile being present, then it will run out of the box. However, it does add some complexity to the app's startup code.

The suggestion was one of a few made by Linus Torvalds on a git or kernel mailing list, but I have not been able to find it today.

这个解决方案与上面 Alan W. Smith 的回答非常相似,但有一点不同:如果应用程序可以在没有app.conf文件的情况下启动,那么它将开箱即用。但是,它确实给应用程序的启动代码增加了一些复杂性。

该建议是 Linus Torvalds 在 git 或内核邮件列表中提出的少数建议之一,但我今天找不到它。

2. Use environment variables and/or command line arguments

2. 使用环境变量和/或命令行参数

You can use an environment variable to point your app at a specific config file. You might start your app like this:

您可以使用环境变量将您的应用程序指向特定的配置文件。你可以这样启动你的应用:

CONFIG_FILE=test.conf ./start-app

or alternatively:

或者:

./start-app --config=test.conf

This means you can have multiple config files development.conf, staging.confand production.conf. When you start the app you tell it which config file to use.

这意味着您可以拥有多个配置文件development.confstaging.conf以及production.conf. 当您启动应用程序时,您会告诉它要使用哪个配置文件。

Developers who want to experiment with a different config can point at their own file, e.g. custom.conf.

想要尝试不同配置的开发人员可以指向他们自己的文件,例如custom.conf.

You can also use environment variables or command line arguments to override specific settings:

您还可以使用环境变量或命令行参数来覆盖特定设置:

./start-app --config=default.conf --db-url=... --debug-level=5

3. Different branches

3. 不同的分支

You can keep your default config in your master branch.

您可以在 master 分支中保留默认配置。

Fork off different branches for each of your different environments.

为每个不同的环境分叉不同的分支。

Each branch can modify the default config file as needed.

每个分支可以根据需要修改默认配置文件。

When your master branch updates, merge from master into your specific branches.

当您的 master 分支更新时,从 master 合并到您的特定分支。

Personally, I don't recommend this approach. I think it's harder to maintain.

就个人而言,我不推荐这种方法。我认为它更难维持。

回答by Matt Gibson

Git will ignore files that you don't explicitly add, so checking different branches out just leaves them where they are in the directory structure as the other files change around them. If you add your config file to the .gitignore file in your repo's root (may have to create it, more info here) then you can still do all-files commands like

Git 将忽略您没有明确添加的文件,因此检查不同的分支只会将它们留在目录结构中的位置,因为其他文件在它们周围发生变化。如果您将配置文件添加到存储库根目录中的 .gitignore 文件(可能需要创建它,更多信息请点击此处),那么您仍然可以执行所有文件命令,例如

git add .

if you want to and not worry about it.

如果你愿意而不用担心的话。

回答by jribeiro

Best way is create a file called ".gitignore" and insert the files/folders you want to ignore.

最好的方法是创建一个名为“.gitignore”的文件并插入您要忽略的文件/文件夹。

This way you can make git add * everytime

这样你就可以让 git add * 每次