I'm trying to get a handle of the merge functionality in git. I've made a simple example to play with and am testing, but I'm unable to achieve the desired results. I begin with the following very simple file
# calc.py
def add():
pass
def substract():
pass
I then git init
, git add .
and git commit -a -m "Initial commit"
. This is now my master. I branch off it to create two branches multiply
and divide
. I git checkout divide
and perform some work so that my file in the divide
branch looks like.
# calc.py
def add():
pass
def substract():
pass
def divide(x, y):
return x / y
I then I :
git checkout master
git merge divide
git branch -d divide
This is followed by doing some work on the multiply
branch which has been branched off from the initial commit and doesn't have the divide
branch changes added to it. Nevertheless, I add a multiply function so that on my multiply
branch my file resembles:
# calc.py
def add():
pass
def substract():
pass
def multiply(x, y):
return x * y
I then want to merge everything together into master
, so I:
git checkout master
git merge multiply
but this results in:
Auto-merging calc.py
CONFLICT (content): Merge conflict in calc.py
Automatic merge failed; fix conflicts and then commit the result.
so that the file resembles:
# calc.py
def add():
pass
def substract():
pass
<<<<<<< HEAD
def divide(x, y):
return x / y
=======
def mulitply(x, y):
return x * y
>>>>>>> mulitply
According to the git merging documentation this is the expected behaviour. I know that I can simply remove the added annotation and commit
.
Is there anyway to merge these examples without having to manually intervene? I was thinking of using the --strategy
flag but I can't succussfully automatically merge the files after master has been changed.
You're experiencing a Git merge conflict that requires human intervention.
That is, Git has encountered a scenario that it understandably cannot auto-merge. Does it keep both sets of changes? If not, does it keep the changes from divide
or from multiply
? If it is to keep both sets of changes, does Git put the divide
changes above or below the multiply
changes? Human intervention is necessary to answer these.
Git merge strategies cannot resolve the conflict in a way that honours both change sets. What merge strategies can do is specify how to handle whitespace and file renames, instruct Git to merge more than two heads, and/or ensure that one side always wins in case of conflict. Merge strategies cannot resolve conflicts in a way that honours two conflicting change sets.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.