From the course: Git: Branches, Merges, and Remotes

Merge conflicts

From the course: Git: Branches, Merges, and Remotes

Start my 1-month free trial

Merge conflicts

- [Instructor] We learned how to merge one branch into another branch, the process went smoothly and was pretty simple, unfortunately it's not always that easy, we need to learn about merge conflicts and how to resolve them. A conflict occurs when there are two changes in the same line or set of lines in two different commits. So let's imagine that I have a line, inside a file in my master branch and that line says Git is great and it has html span tags around it. Now let's imagine that I make a new branch called new styles, after I've made that branch myself or someone else makes an edit to the file in the master branch and commits it, and that change is to change the span tags into strong tags, now at the same time myself or someone else in the new styles branch makes an edit to the exact same line, but makes a different change. This time instead of putting strong tags around it they put emphasis tags or em tags around it. So now when we go to merge these branches together what is Git supposed to do? It's getting two sets of instructions, it has one commit in the master branch that says change it to strong tags, and it has a commit in the new styles branch that says change it to em tags. If these changes had been to two different lines in the same file there would not be a conflict. Git would be able to incorporate both changes into the result, it would combine them smoothly. But it's when you have two edits to the same line, that Git doesn't know what to do. and then wait for you to fix the problem. This is an example of a merge conflict, in order to demonstrate we need to create a merge conflict, so let's do that let's make sure you're on your master branch already, you can see that I am. If not you'll want to check out the master branch, we can see it there and we're already switched to it. and let's make some changes, we're going to make our changes to the mission.html file to where the text is, just going to kind of hide some of this so I can get down here, there's this big block to line 63 here. Let's take away unlike any other, So that's an edit to line 63, and will provide you will an opportunity. Well that's a typo, that should be with an opportunity. So let's fix that typo in that line, here let's make it we have and little further down here it says we ask ourselves one question. Let's say we ask ourselves a question, Now make sure that you make an edit to this line. where I'm going to try to create a conflict. So now I've committed that to my text edits branch. file again and you'll see that all of our edits are gone, let's go through and make a few edits here as well. We'll provide you with I'll make the same edit here, and then let's come down here a little further and instead of having the and quote marks here. Let's type ldquo that's left double quote and over here we'll make this a right double quote. Let's add a few more single quotes as well, let's do curly quotes here and we'll make this a right single quote, character entity, and I'll copy that let's put the same thing here, and let's scroll down a little more. I just saw it, let's make that change there. Alright I may have missed some, but I got most of them. let's type git commit dash am replaces straight quotes with curly quotes, okay so now I have another commit here and if we do git log dash dash oneline I'll just do five. We can see those lines, here's the commit that I just made, notice the commit before it is merge branch, shorten title in that was what I was doing right before that, now let's hit the up arrow and let's just take the same look at text_edits and you can see it's top five commits it also has that merge branch shorten title commits it also has that merge branch shorten title but it has a different commit right here. but it has a different commit right here. So they have two different commits, So they have two different commits, they come after that 2223130 commit. they come after that 2223130 commit. That's where our conflict is going to be, That's where our conflict is going to be, alright so now let's try to merge them. alright so now let's try to merge them. Git merge I'm on the master branch Git merge I'm on the master branch and I'm going to merge in text_edits. and I'm going to merge in text_edits. Auto merging, conflict, content. Auto merging, conflict, content. There's a merge conflict ion mission.html There's a merge conflict ion mission.html automatic merge failed, fix conflicts automatic merge failed, fix conflicts and then commit the result. and then commit the result. Notice also that now my branch name is just not called Notice also that now my branch name is just not called master anymore, it's master and then it has a line master anymore, it's master and then it has a line and then it says merging, letting me know that and then it says merging, letting me know that I'm in the middle of a merge. I'm in the middle of a merge. That's very handy too, let's type git status That's very handy too, let's type git status let's see what it tells us, I'm on branch master, let's see what it tells us, I'm on branch master, but I have unmerged paths so I need to fix but I have unmerged paths so I need to fix the conflicts and then commit them, the conflicts and then commit them, and I can use git merge abort to abort the merge and I can use git merge abort to abort the merge we'll look at that in the next movie we'll look at that in the next movie and then it tells me what the problems are. and then it tells me what the problems are. Here's the problem mission.html both files Here's the problem mission.html both files have been modified and I need to use git add have been modified and I need to use git add to mark it once I've resolved it. There's no changes added and we're ready to take a look. There's no changes added and we're ready to take a look. Alright now let's go look at mission.html and inside this file, you should see something like this. You'll see a bunch of left facing arrows You'll see a bunch of left facing arrows or less than signs followed by the word head, or less than signs followed by the word head, that's letting me know that's where my current head is that's letting me know that's where my current head is I'm on the master branch so that's what the current state of the master branch is, if we scroll down then we'll see a bunch of equal signs if we scroll down then we'll see a bunch of equal signs and then we see the same text again. We are passionate about California and so on, We are passionate about California and so on, we are passionate about California and so on. Right we're seeing that here, and then down here we have a bunch of right facing arrows, or greater than signs followed by the name of that branch or greater than signs followed by the name of that branch text_edits the thing that we're merging in. text_edits the thing that we're merging in. So what it's doing is it's showing us both versions, So what it's doing is it's showing us both versions, this is a very common format for flagging merge conflicts to you, these are called conflict markers. So in the next movie we're going to look at these, and learn how to resolve them. and learn how to resolve them. But just understand for now, this is the part that's in our current branch, and then everything that's down here below these lines is what's in the branch below these lines is what's in the branch that we're merging in, now that we've created a conflict that we're merging in, now that we've created a conflict and we can see how Git handles it, in the next movie let's learn how we can resolve it. in the next movie let's learn how we can resolve it.

Contents