Git:分支合并主人和新分支之间的冲突

时间:2016-05-11 17:56:00

标签: git

我有2台服务器:example.com和example.net

我有2个git分支:master(example.com)和beta(example.net)

现在,主分支中有一个名为config.php的文件,其中包含数据库详细信息:

$db_name : 'com_dbname';

当我创建beta分支时,我需要更改此行,以便它可以在example.net上运行:

$db_name : 'net_dbname';

所以,config.php现在与每个分支不同。现在这两个分支都已提交并推送到远程存储库。

并且主分支上的开发过程仍在继续。我需要在$db_name

下添加一些行
$db_name : 'com_dbname';
$other_var : 'other_value';

通常我可以在我的bitbucket帐户上将主人合并到beta分支。但由于此文件已更改,我无法将master合并为beta分支。它的内容如下:

此合并具有必须在提交之前解决的冲突。 要手动将这些更改合并到测试版,请运行以下命令:

$ git checkout 94c22fbaa758
# Note: This will create a detached head!
$ git merge remotes/origin/master

这是我终端上的输出:

Note: checking out '94c22fbaa758'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at 94c22fb... 

git merge remotes/origin/master
Already up-to-date.

我很困惑。它说一切都已经是最新的,但我仍然在我的bitbucket上看到1 commit behind master.,仍然无法将主人合并到beta分支。

请帮助我。我真的不知道如何解决这个问题,而且我在最近2个月前使用git并且从未经历过这种分支合并的情况。非常感谢你。

1 个答案:

答案 0 :(得分:1)

我通常使用配置文件的一个解决方案是包含本地文件(如果存在),忽略它来自版本控制。

所以,让我们假设您正在开发一个php项目,并且您有一个在主分支中使用的默认配置文件。将其包含在项目中的代码可能如下所示:

<?php
include("config.php");

现在,为了使用本地配置更改覆盖值,您可以先检查它是否存在,然后再加载它以覆盖默认配置文件中建立的值。

<?php
include("config.php");
if (file_exists("config.local.php"))
   require_once("config.local.php");

或者从版本控制中忽略它,如果您确定只从master合并到beta,这是非常不可能的,特别是考虑到beta分支名称背后的含义,那么您可以将config.local.php提交到beta分支,将master合并到beta时也不会发生冲突。但请注意,如果您从beta合并到master,这绝对会突然显示并覆盖您的默认配置,这就是我建议忽略local中的.gitignore配置文件的原因。不可否认,跟上环境特定的差异是一件很麻烦的事情(这不是你首先选择版本控制系统的一个原因吗?),但是从这个例子中你可以看到#39 ;只是真的需要改变db_name,事实证明这并不是我的经验中的重大交易。另外,您可以将此视为保护不打算在其他任何地方使用的凭据的额外奖励,因此当您与团队共享此代码时,在版本控制中不跟踪它们是有意义的。

相关问题