FREELessons: 24Length: 3.1 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

6.4 Cleaning Up Merge Conflicts

We've already discussed merging branches together. But it won't always be as simple as the merge that we did. If you're doing any significant amount of work on a branch and you're working on multiple branches at once. It probably won't be long before you run into a merge conflict. This happens when the same content has been changed on both of the branches that you're merging. So Git doesn't know which content to use. So let's go ahead and create a merge conflict and then look at how we would clear that up. Currently our project has the master branch and the GitHub pages branch. And if we do a git status, you can see that our working directory is clean. Notice that I'm using some of the aliases we created. So let's start by making a bug fix branch. So we're gonna have git checkout -b to create a new branch. We'll just call this bug-fix. And now on the bug-fix branch, we're gonna open up fix.txt. And we're just gonna change line four from fix this terrible bug to fix that terrible bug, and then we're just gonna git add that and git commit it with the message change this to that. All right, so now we've made a commit on our bug-fix branch. Let's move back to the master branch, using our git checkout alias. And let's change the very same line in the fix.txt file. Except, this time we'll just add a bunch of exclamation points to the end of it. So to fix this terrible bug !!!!!, we'll add that and then git commit. And we'll just say, made this fix important. All right. So now if I clear the screen and I run our graph that we've created previously, I made an alias for that too. You can see that from the commit here that has the message merge branch bug. We've branched off with the bug-fix branch. We've made a commit there, and we've also made a commit on the master branch. So we've made a commit on two different branches and intentionally in this case, but maybe not intentionally in your actual project, we changed the same line of code on both branches. So now we're gonna try and merge the bug-fix branch in and let's see what happens. So if I run git merge bug-fix, it looks like our merge did not go so well. So we have the Auto-merging fix.txt, then CONFLICT, Merge conflict in fix.txt, and then we have some instructions. Automatic merge failed fix conflicts and then commit the results. So we're told there's only one file that had a merge conflict in it, that's fix.txt, and we're supposed to fix those conflicts and then commit the result. So if I open up fix.txt, notice what we have here. We have the content of both files. Since we're on the master branch, HEAD is pointing to the last commit that we made on the master branch. And in that last commit, line five here is what the content of this line looked like. Down here, you can see that the bug-fix branch had this line looking like this. Fix that terrible bug, instead of fix this terrible bug!!!! So git gives us the two different lines with this syntax showing us which line came from which branch. And now it's our responsibility to put this file into the right state, to change it so that it looks like exactly what we want it to look like. So we're gonna actually combine these lines by changing this to that. And then we're gonna leave the exclamation points there. And now of course we want to get rid of all the extra lines. The other lines of content that git had as well as the conflict Chrome if you will that git added in there. So now I'm gonna save this. If I run git status, you can see that this is an unmerged path and we have to add it to the staging directory. So I'll do a git add. and then git commit. And If I just run git commit without passing it a message, we're taken to our editor, as we've seen previously. And notice right here that merge branch bug-fix is what is provided as the default commit. So it recognizes the fact that you tried to do a merge. We werent able to finish the merge, so that merge commit was never actually made. However, now you've corrected that, now you're ready to make the commit for that merge. So I'm gonna leave this as the merge branch bug-fix commit message. I will save and close our editor and now you can see that merge branch bug-fix was created. So if I clear the screen and show our graph, you can see that the latest commit brought these two branches together and we have merged branch bug-fix. So there you go. There's a simple example of how a merge conflict works. Obviously when you're working on an actual project you may have more than one line that's conflicting or you may have conflicts in multiple lines, but it really is that straightforward. You'll try to do the merge, Git will do as much as it can but tell you there is a merge conflict so go make sure these files are in the state you want them to be. Once they are, you just stage them and commit them, and there you have your merge commit. So as you can see, Git takes what could be a messy situation and makes it very easy for you to clean up that conflict and move ahead with your project.

Back to the top