简体   繁体   English

合并时Git分支会覆盖master而不会产生冲突

[英]Git branch overwrites master when merging without offering conflicts

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. 当我将master与分支合并时,分支中的代码会覆盖master中的代码,我想只在master中插入不同的代码,或者至少要询问我想保留哪些代码。 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: 在项目目录中我有一个名为index.php的文件,代码如下:

<?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: 我把这个代码放在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. 然后分支代码将覆盖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 ? 不应该git让我选择保留哪些代码,或者有什么方法可以强迫它? 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. 然后git将执行递归策略合并,并将从分支中取出MyClass并保留master中的所有其他内容。

I do not know if I am showing you good examples, let me try to explain the situation once again: 我不知道我是否向你展示了很好的例子,让我试着再次解释一下情况:

1) I have some code in master that do not exists in branch . 1)我有一些代码master不中是否存在branch
2) I have some code in branch that do not exists in master . 2)我有一些代码branch不存在中的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 ? 如果不能以一些干净,计划好的方式发生这种情况,我是否至少可以强迫git问我想要对这些差异做些什么? I can only mange to make branch override master, and that is bad. 我只能管理分支覆盖主,这很糟糕。

If I understand correctly, then you are checking out branch , replacing some of the code from master , and then merging branch into master . 如果我理解正确,那么你正在检查branch ,从master替换一些代码,然后将branch合并到master

The whole point of making a branch is so that you can make changes to code, and then merge those changes in. 制作分支的重点是,您可以对代码进行更改,然后合并这些更改。

Maybe an example will help. 也许一个例子会有所帮助。

Let's say you had a file on master that only had the content 假设您在master上有一个只有内容的文件

This is good code

And then you checked out branch and changed the file to say 然后你检查了branch并更改了文件说

This is great code

Now, you checkout master again. 现在,你再次结帐master Right now, the file is 现在,文件是

This is good code

If you merge, then the file will change to 如果合并,则文件将更改为

This is great code

However, if, before merging, you changed the file to say 但是,如果在合并之前,您更改了文件

This is really amazing code

And then tried to merge, then git would tell you that there was a conflict, and would not automatically merge these files. 然后尝试合并,然后git会告诉你存在冲突,并且不会自动合并这些文件。

This might help you to get the concept. 可能会帮助您获得概念。 Basically, because master had not changed at all since branch was created, git can assume that any changes you made were on purpose. 基本上,因为master在创建branch后根本没有改变,所以git可以假设您所做的任何更改都是故意的。

Merging a branch means that you want to add the changes suggested by the branch. 合并分支意味着您要add分支建议的changes 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 要查看diff b / w两个分支在合并之前要检查的内容,请运行

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. 如果您觉得my_branch的代码不符合您的标准或者是错误的,请不要合并它。

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

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