In summary, a merge conflict happens when you merge two separate branches that contain different changes on the same locations in the same file and it's unclear which change should take precedence or whether both should be combined.
![git create branch conflicts git create branch conflicts](https://api.devtutorial.io/uploads/2020/10/5f77435371032.png)
This merge conflict will also occur even when you attempt the merge in the opposite direction, by merging controller into branch2. Git is unable to determine whether a) the incoming change from branch2 should override the second line, b) the current change in controller should override the incoming change, or c) there should be some kind of combination of both changes. And yet both are changing the same line in the same file differently. Why? Because now both controller and branch2 have commits in their respective histories about adding a second line to controller.txt. Now, if we want to merge branch2 into controller, we'll encounter a merge conflict: Or, presented as a flow diagram, like this: Now the controller.txt in controller branch looks like this: a This is because the history for branch1 is simply add 1 to the second line of controller.txt. So when we merge branch1 back to controller branch, the merge is successful. And the changes in branch2 are from a to a 2 in controller.txt. To recap: The changes in branch1 are from a to a 1 in controller.txt. In branch1, we change controller.txt to read: aĪnd in branch2, we change the original controller.txt into this: a
#GIT CREATE BRANCH CONFLICTS CODE#
Imagine we have a local code repository like this:Īnd we have a single text file called controller.txt in the controller branch that simply says this: a This is easier to explain with an example. Regardless, the conflict happens at the destination branch of your merge. Merge changes in one remote branch to another remote branch
![git create branch conflicts git create branch conflicts](https://nishantrana.files.wordpress.com/2019/10/101919_1457_workingwith1.png)
Merge changes in one local branch to another local branch Merge outgoing changes from local branch to remote branch Merge incoming changes from remote branch to local branch
![git create branch conflicts git create branch conflicts](https://confluence.atlassian.com/get-started-with-sourcetree/files/847359121/847753558/2/1520272028299/merge_git.png)
Since typically you have local and remote repositories, the following scenarios can occur: When merging changes from one branch (the source branch) to another branch (the destination branch), there's always the possibility that the merge will cause a conflict at the destination. The term "merge conflict" obviously contains the word "merge"-meaning that, to generate a merge conflict, you need to have a merge taking place. Without further ado, let's get going! When There's a Merge Conflict, in Which Branch Does the Conflict Take Place? But sometimes, it's good to have all the answers in one place so we get an overview of the landscape. Many of these frequently asked questions could be their own standalone articles. This time, I'm going to mix things up with an FAQ on the subject. Previously, I wrote a long-form post about merge conflict.