Git分支在合并时会覆盖主机而不会产生冲突 [英] Git branch overwrites master when merging without offering conflicts
问题描述
我有一个主项目和一个分支。当我将master与branch合并时,branch中的代码覆盖master中的代码,并且我希望仅在master中插入不同的代码,或者至少询问我想保留哪些代码。以下是我如何设置所有内容的一个简单示例:
mkdir项目
cd project
git init
在项目目录中我有一个名为index.php的文件,其代码如下:
<?php
/ **
*这是主代码。
* /
class ClassName extends AnotherClass
{
函数__construct(参数)
{
//这是来自
}
}
然后我做分支:
$
然后我把这段代码放到index.php里面: git checkout -b my_branch
/ p> <?php
/ **
*这是分支代码。
* /
class ClassName extends AnotherClass
{
function __construct(argument)
{
//这是从分支
}
}
然后我签出并尝试合并:
git checkout master
git merge my_branch
然后分支代码会覆盖master中的分支代码。在我的主人,我会有像分支一样的代码。 git不应该让我选择保留哪些代码,还是有什么办法强制这样做?如果没有,我做错了什么?
如果我在分支代码中进行此更改:
class ClassName extends AnotherClass => class ClassName extends MyClass
然后git会执行递归策略合并,并且会花费 MyClass
从分支机构,并保持一切从主。
我不知道我是否向你展示了很好的例子,让我试着再次解释这种情况,请裸机我:
<1>我在 master
中有一些不存在于分支中的代码
。
2) branch
中有一些代码不存在于 master
。
我应该如何处理这个问题,而不是在主分支中丢失不同的代码?如果这不可能发生在一些干净而有计划的方式上,我是否至少可以强迫git问我想要处理这些差异?我只能管理分支重写master,这很糟糕。
我真的很困惑。谢谢
合并分支意味着你想要 add
code>更改由分支建议。您可以不选择性地合并分支。它得到的整体合并。
要查看合并前检查的两个分支的差异,运行
git diff master my_branch
如果你不喜欢 my_branch
达到您的标准或错误,请勿合并。
I have master project and one branch made from it. When I merge master with branch, code in branch overwrites the one in master, and I would like to insert only different code from branch in master, or at least to be asked which code I want to keep. Here is one simple example of how I set up everything:
mkdir project
cd project
git init
inside project directory I have one file called index.php with this code:
<?php
/**
* This is master code.
*/
class ClassName extends AnotherClass
{
function __construct(argument)
{
// this is from master
}
}
Then I make branch:
git checkout -b my_branch
And I put this code inside index.php:
<?php
/**
* This is branch code.
*/
class ClassName extends AnotherClass
{
function __construct(argument)
{
// this is from branch
}
}
Then I checkout to master and try to merge:
git checkout master
git merge my_branch
And then branch code will override the one from master. In my master I will have same code like in branch. Shouldn't git offer me to chose which code to keep, or is there any way to force that ? If not, what I am doing wrong ?
If I make this change in branch code:
class ClassName extends AnotherClass => class ClassName extends MyClass
Then git would do recursive strategy merge, and would take MyClass
from branch and keep everything else from master.
I do not know if I am showing you good examples, let me try to explain the situation once again, please bare with me:
1) I have some code in master
that do not exists in branch
.
2) I have some code in branch
that do not exists in master
.
How should I deal with this and not lose that different codes in both master and branch ? If that can not happen on some clean, good planned way, can I at least force git to ask me what I want to do with those differences ? I can only mange to make branch override master, and that is bad.
I am really confused. Thanks
Merging a branch means that you want to add
the changes
suggested by the branch. You can NOT selectively merge a branch. It get's merged as a whole.
To see what is the diff b/w two branches for reviewing before merging, run
git diff master my_branch
If you don't feel like the code in the my_branch
is up to your standards OR is errorenous, don't merge it.
这篇关于Git分支在合并时会覆盖主机而不会产生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!