Git branch overwrites master when merging without offering conflicts

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

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:

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.

回答1:

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.



回答2:

If I understand correctly, then you are checking out branch, replacing some of the code from master, and then merging branch into 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

This is good code 

And then you checked out branch and changed the file to say

This is great code 

Now, you checkout master again. 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.

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!