简体   繁体   English

如何在Web应用程序的不同开发阶段使用git分支?

[英]How to use git branches for different development stages of a web app?

I'm mostly new on Git, and I just recently learned the basics of git branching. 我主要是Git的新手,最近才学习git分支的基础知识。 One of my troubles now is how to set up git branching for use on my web project. 我现在的麻烦之一是如何设置git分支以用于我的Web项目。

I use the main branch for production, and another one called beta for live changes and feature testing. 我将主分支用于生产,将另一个分支称为beta用于实时更改和功能测试。 This branch lives on a subfolder of the webserver, accesible via the beta. 该分支位于Web服务器的子文件夹中,可通过beta. subdomain. 子域。

The main problem I have is that, as a web project, some URLs, API Keys, etc are defined on the code as constants. 我的主要问题是,作为一个Web项目,一些URL,API密钥等在代码上被定义为常量。 In order to the project to work properly, I have to change the API keys and URLs on code, and that changes will be obviously pushed to the main project when I merge the branches. 为了使项目正常工作,我必须更改代码上的API密钥和URL,并且当合并分支时,这些更改显然会推送到主项目中。

I have no idea how to issue this, without having to edit those files manually on master each time I merge it with beta , and dealing with conflicts on the next merge. 我不知道如何发布此文件,而不必每次与beta合并时都必须在master手动编辑这些文件,并在下次合并时处理冲突。

Do you have any hints about how to face this? 您对如何面对这个有任何暗示吗? Any response will be appreciated ^^ 任何回应将不胜感激^^

Check those 2 links: 检查这两个链接:

For different configurations. 对于不同的配置。 You can create such folder structure: 您可以创建这样的文件夹结构:

-configs
  -dev
    conf.ini
  -beta
    conf.ini
  -live
    conf.ini
-src
    conf.ini

Put all your config files into this folders according to env and commit them. 根据环境将所有配置文件放入此文件夹并提交。 After it remove all your files( src/conf.ini ) from index and put them under gitignore and after it you can make changes at config folders and copy those files to original. 从索引中删除所有文件( src/conf.ini )并将其放在gitignore下之后,您可以在配置文件夹中进行更改并将这些文件复制到原始文件中。 They will not be modified, because they are under gitignore . 它们不会被修改,因为它们在gitignore下。

The core idea for something like this is to make sure you use separate commits for the changes you want to propagate from BETA to PRODUCTION vs the changes you don't want to propagate. 诸如此类的核心思想是确保对要从BETA传播到PRODUCTION的更改和不希望传播的更改使用单独的提交。 You should merge normally any commits you want to propagate, but you "fake merge" the commit(s) you don't. 通常,您应该合并要传播的所有提交,但要“假合并”不需要的提交。

You should be comfortable with merging before you use a merge workflow like this. 在使用这样的合并工作流之前,您应该对合并感到满意。 You need to realize that each time you merge BETA into PRODUCTION, you will be incorporate ANY and ALL commits made in BETA (that haven't already been merged) into PRODUCTION. 您需要意识到,每次将BETA合并到PRODUCTION中时,您都会将在BETA中进行的任何和所有提交(尚未合并)合并到PRODUCTION中。 Once a commit has been merged (or more technically speaking, once the commit is reachable from PRODUCTION), it won't be merged again in the future. 一旦合并了提交(或更确切地说,是从PRODUCTION可以访问的提交),以后就不会再次合并。 There's a way to "fake merge" a commit so it will appear to have been merged in without really applying changes and so it will be ignored in future changes. 有一种“伪合并”提交的方法,因此它看起来好像没有真正应用更改就被合并了,因此在以后的更改中它将被忽略。

For a workflow like yours, you would do something like this: 对于像您这样的工作流程,您可以执行以下操作:

$ # Create BETA branch
$ git checkout -b BETA PRODUCTION

$ <make BETA-only changes>
$ git commit -m "BETA-only changes"                       # => commit #3612072

$ # "Fake merge" all changes from BETA that have not already been merged into PRODUCTION,
$ # i.e. "mark" those changes as having been merged without really merging them
$ git checkout PRODUCTION
$ git merge -strategy=ours -m "Fake merge BETA => PRODUCTION" BETA
$                                                         # => commit #8eae339
$ # Okay - now future merges will ignore that commit

$ # Make changes in BETA branch
$ git checkout BETA
$ <make changes>
$ git commit -m "Real change in BETA"                     # => commit #3cd69ad

$ # Real merge all changes from BETA that have not already been merged (or fake merged) into PRODUCTION
$ # i.e. this should ignore the changes you "fake-merged" before
$ git checkout PRODUCTION
$ git merge -m "Real merge BETA => PRODUCTION" BETA       # => commit #bc26f91
$ # Okay - PRODUCTION should have just the most recent changes and not the BETA-only changes

Rinse and repeat. 冲洗并重复。 Ideally, you won't make any more BETA-only changes and it will "just work". 理想情况下,您将不再进行仅适用于BETA的更改,并且它将“正常工作”。 If you do need to make future BETA-only changes, make sure those changes are committed in their own commit and then "fake merge" those changes 如果您确实需要进行仅适用于BETA的更改,请确保这些更改以自己的提交方式提交,然后“伪合并”这些更改

Your repository should end up looking like: 您的存储库应最终看起来像:

*   bc26f91 Oct 31 Real merge of changes from BETA into PRODUCTION
|\
| * 3cd69ad Oct 31 Real change in BETA
* |   8eae339 Oct 31 Fake merge BETA => PRODUCTION
|\ \
| |/
| * 3612072 Oct 31 BETA-only changes
| |

=== ===

Edit: A similar issue crops up when changing version numbers in branches, eg advancing the version # for a release in your maintenance branch and then pulling that bugfix into your mainline development branch: 编辑:更改分支中的版本号时会出现类似的问题,例如,在维护分支中推进版本号,然后将该错误修正拉入主线开发分支:

Git merge strategy for integrating 'patch' branch into 'minor', 'minor' branch into 'major' Git合并策略,用于将“ patch”分支集成到“ minor”中,将“ minor”分支集成到“ major”中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM