How to solve file conflict while using git&github

Today, I will use images to explain how to slove conflict while using github.

There are many git or github related tutorials, but I could't find the one that suitable for me, which means none of them can make me understand.

So, I asked my colleague how I can solve this problem step by step, with paper and pen. Now, let's assume you have done half way of your work and here is the master branch on remote and local.

f:id:SallyHasNoNose:20161113181306p:plain

The two branches are synchronised. And now, you are gonna implement a new feature, let's call feature B which leads you to create a new branch called B. You start to change/add/delete something then you do "git add" and "git commit".

f:id:SallyHasNoNose:20161113181520p:plain

Now, as usual you do "git push" the branch B to remote github.

f:id:SallyHasNoNose:20161113181944p:plain

Github will record your local commits. You create a pull request and now are ready to ask someone to check your code. However, in our case, that somebody who is gonna check your code is out of the town and you can't wait him because you have to catch up with the deadline. So on your local, you continue working on another new feature, let's say feature C.

f:id:SallyHasNoNose:20161113182301p:plain

And after some changes and commits you decided to push this branch to github.

f:id:SallyHasNoNose:20161113182406p:plain

Ok, everything looks good. And you feel happy because that guy who is gonna check your code just came back and he commented "LGTM" then he merged your pull request.

f:id:SallyHasNoNose:20161113182557p:plain

He then checked your feature C implementation nad he is about to merge it into master branch. But, here, the problem is you changed a same file in branch B and branch C so the conflict occurs!

f:id:SallyHasNoNose:20161113194731p:plain

This is because B and C changed same file which is from A. Now branch C can not merged onto master branch. Next I will explain how to solve this problem in a better way without "git rebase"

First, on your local checkout to the master branch. And pull the latest master branch from remote.

f:id:SallyHasNoNose:20161113195301p:plain

Then, you checkout to branch C on your local, merge the master branch.

f:id:SallyHasNoNose:20161113195422p:plain

Right now, since your branch C and your master branch has the same file changed on both side, there should be a conflict just like on the remote branch. You can check the conflict part from that file. 

f:id:SallyHasNoNose:20161113195623p:plain

You found the conflict part, you fix the code to the newset version, then you push(in this case branch C) to the remote github, then now you can merge the branch C to master branch. Problem solved!

f:id:SallyHasNoNose:20161113195754p:plain