In this video, discover cases where merging is complicated because each version of the code has changes that conflict.
- [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 but it has a different commit right here. So they have two different commits, they come after that 2223130 commit. That's where our conflict is going to be, alright so now let's try to merge them. Git merge I'm on the master branch and I'm going to merge in text_edits. Auto merging, conflict, content. There's a merge conflict ion mission.html automatic merge failed, fix conflicts and then commit the result. Notice also that now my branch name is just not called master anymore, it's master and then it has a line and then it says merging, letting me know that I'm in the middle of a merge. That's very handy too, let's type git status let's see what it tells us, I'm on branch master, but I have unmerged paths so I need to fix the conflicts and then commit them, and I can use git merge abort to abort the merge we'll look at that in the next movie and then it tells me what the problems are. Here's the problem mission.html both files have been modified and I need to use git add There's no changes added and we're ready to take a look. You'll see a bunch of left facing arrows or less than signs followed by the word head, that's letting me know that's where my current head is if we scroll down then we'll see a bunch of equal signs We are passionate about California and so on, or greater than signs followed by the name of that branch text_edits the thing that we're merging in. So what it's doing is it's showing us both versions, and learn how to resolve them. below these lines is what's in the branch that we're merging in, now that we've created a conflict in the next movie let's learn how we can resolve it.
- Finding and viewing commits
- Creating and switching branches
- Comparing branches
- Resetting branches
- Merging branches
- Stashing uncommitted changes
- Setting up a remote repository
- Creating remote branches
- Fetching and pushing changes to a remote
- Enabling remote collaboration