Conflicts
Solving conflicts in our code.
Conflicts
Solving conflicts in our code.
0
0
Checkbox to mark video as read
Mark as read

It's possible that when we try to perform a merge we encounter conflicts that prevent us from completing the action. This happens when there are commits with changes on the same lines of code in both branches.

Let's look at an example where we have two branches named branch_one and branch_two and we edit the same line of code in both branches. To do this:

  • We create branch_one from master.
  • We create branch_two from branch_one.
  • We edit our code and make a commit in branch_two.
  • We switch to branch_one and edit the same part of the code but with different changes. Then, we make a commit.

The result is a change in each branch made on the same line of code.






If we try to merge branch_two into branch_one, since Git doesn't know which change is correct, we'll see a message explaining that we need to resolve the conflicts before we can complete the merge.

Files with conflicts will be marked with a warning icon. When we select the file, we will see the two versions of the changes that are causing the conflict. As shown in the image, one version is labeled as "HEAD" (the current branch, the destination), and the other is labeled with the branch we are trying to merge (branch_two).



Resolving conflicts

The only way to resolve a conflict is to choose which code to keep. SourceTree offers the option to do this with just a few clicks, allowing us to choose whether to keep the changes from "mine" (the current branch) or "theirs" (the branch we are trying to merge). To do this, simply right-click on the file with conflicts and select Resolve Conflicts -> Resolve Using 'Mine', which in this case will keep the changes made in branch_one.



This will resolve all conflicts in that file with the same option.

Finishing the merge

Once all conflicts are resolved, we'll have a pending commit with an auto-filled message. To complete the merge, simply perform a normal commit as we've done so far.


Manually resolving conflicts

Sometimes, if the conflict is complex, we may need to keep parts of both branches. In this case, we can resolve the conflict by opening the file and manually editing it, removing the lines that Git has added.

The following lines should never remain in our code:

  • <<<<<<<< HEAD
  • ========
  • >>>>>>>> branch_name

0 Comments

Join the community to comment

Be the first to comment

Accept Cookies

We use cookies to collect and analyze information on site performance and usage, in order to provide you with better service.

Check our Privacy Policy