This is your definitive guide to merge conflicts, why they happen, how to fix th em, tips & tricks to make your life easier and we're going to
do it all with motion graphics and code samples like you're seeing on the screen to demystify
the tricky world of merge conflicts. Whether you primarily use the command-line
or a code editor like VS Code this is the video you've been looking for. Let's get started. The most common type of Git merge that results in conflicts are what's called a 3-way merges. As you can see in this example, a commit has
been introduced to main branch after the feature_branch was created. This means that when I pop open my terminal
to run merge, Git will need to compare the tips of both branches here and here, plus
take into account their common ancestor here to attempt to resolve any conflicts. 1, 2, 3 and that's a 3 way merge. As you can see, this is how a 3-way merge
is suppose to work, and in this case we didn't get a merge conflict, but there are a lot of moving parts, so let's rewind and look at a scenario that would result in a conflict. Conflicts typically occur when branches modify
the same part of a file that exists on both branches. If I examine the files in each of these commits
we can see they both made changes to the index.html file. And further more, I can see they both modified
the same part of the file. When I attempt to perform a merge, Git will
try to automatically merge those index.html files. If Git still doesn't know what to do, and can't automatically perform the merge itself it will throw a merge conflict. It's also worth mentioning that Git can usually merge files on it's own, even if those files were both modified. Conflicts occur when this auto-merging breaks down, but you can actually customize what algorithm Git uses to do this auto-merge. It might be worth reading about if you get a lot of conflicts. But, that's neither here nor there because once we have merge conflicts we have to fix them. Let's take a peak at what that might look like. I'll run merge, and boom conflict. The first of the two most common types of
merge conflicts. In this case, Git didn't even try to start
the merge operation because it noticed I have uncommitted changes in my working directory. If I run status to reveal these local changes,
you can see I've actually been working on the index.html file which explains why Git
was reluctant to start the merge process. This error is easy enough to bypass, simply
set your local changes aside for later using stash like this. This sets the changes aside and clears your working directory so none of the local files interfere with the merge. But stash is pretty interesting on it's own, so if your
curious to dive deeper check out my other video. Now that we've restored our working directory
to a clean slate, I can attempt my merge again. But, dang it, another merge conflict. This time, Git actually tried to merge the
branches, and failed. This is the second and more common type of
merge conflict. Getting this message is a little intimidating,
but trust me, merge conflicts can be solved in a straightforward manner as long as you
know the steps. But speaking of steps, it's pretty nice to
take steps to improve your Git knowledge... especially when those steps involve comprehensive
motion graphics, coding samples and other intuitive learning methods. If you're looking to take your understanding
of Git to the next level, check out LearnGit.io at the link in the description. It's everything you love about this youtube
channel and whole lot more. If you've struggled to understand Git in the
past, LearnGit.io is what you need. Ok, back to the video. The first step is to start by illuminating
our path forward by getting some more information. It can be helpful to know which commits are
in conflict. You can do this with the log command by passing
the --merge flag like this. As you can see, it's these two commits here that contain the conflict. That's cool and all, but what's really actionable
is what file or files need conflict remediation, and we can find that out by running Git status. As you can see, even though our branches and
commits may contain a large amount of files, it's rare that you have merge conflicts in
all those files. In fact, typically you'll only have a few
files that require manual intervention and you can clearly see what files they are in
the output of status command under the "Unmerged Paths" header In this case, I only have one merge conflict
in the index.html file just like we saw in the animation. You can also see the other files that will
be part of this merge, but don't have conflicts. And they'll be in this section here. But before we dive into fixing the conflicts,
you might just realize at this point you're in over your head and want to abort the merge
all together. If that's you, no worries. Just run `git merge --abort` and Git will
return your branch back to it's state before the merge. But, in this video we press on. So now that we know which files have conflicts,
we can dig deeper and figure out what the conflicts actually are. Fortunately, Git has also already done this work for you. To start, we need to open the conflicted file,
either in a command line text editor like VIM, or in your normal IDE like VSCode. For this example I'll just use vim but later
in this video, I'll show you a cool trick with VS Code too. Once your file is open, look for a section
of your file that has these funny markers. These are called conflict markers and are
added by Git in the exact spots in the file that it couldn't merge automatically. At first glance, this looks confusing, but
let me show you how easy it is to read this. After the <<<<<<< marker you'll
have the name of the branch. The first marker will be labeled with HEAD,
in this case HEAD indicates the latest commit on the main branch. This means anything between that line and
the divider ====== is what was added by the main branch. Then anything between that ======= divider and the
closing conflict marker is what was introduced by our feature branch. Now that we know what part of the file Git
couldn't merge automatically, how do you go about resolving this conflict? This too is straightforward. Simply edit the file and replace this entire
section with how these lines should look like after the merge. That could mean keeping the changes introduced
by main, or keeping the changes introduced by the feature branch. In my example here there is some confusion
about the contact me section of my website. I want visitors to check out my youtube channel
but to email me with more specific inquiries, so I'll update the section to reflect this. And that happens to be a combination of both the main branch and feature_branch changes. After that, save the file and repeat this
same process for any file with a merge conflict. Lastly, stage the files you edited with `git
add` and run `git commit` with a commit message to finalize the merge. This commit will be the merge commit so remember
to provide a descriptive message. That's it, you've successfully merged your
branches and resolved your conflicts! But wait, that wasn't as hard as you thought
right? But can it be easier? Yes! Let me show you a trick. Let's rewind to right after we tried to run the
merge. Instead of opening my conflicted file in my
terminal text editor, I'm going to open it in VS Code. As you can see, VS Code has some built in
features that make resolving merge conflicts really easy. First, the conflict area is clearly marked
with these bright colors, and it's really easy to see what change came from which branch. then to resolve the conflict, I have these handy buttons. Clicking these will automatically update the
conflict area in the way I choose. For example, if I wanted to keep the change
introduced by main and throw away the one from my feature branch, I'd click this button. But for this example, I'll just click keep
both, then modify to my chosen outcome. I'll just save, then return back to my terminal
to complete the merge in the exact same way as before. First staging, then commiting the files. And that's it. Let's review: You'll start with a merge where you'll see
a conflict. If you've got a conflict that starts with
"error: your local changes will be overwritten", stash your changes then continue Next, if you have a conflict that looks like
this, start by using `git status` to get information about which files have conflicts. Next open those files and remove the conflict
markers. Then save and commit those files. That's it, you've fixed all your merge conflicts. And that's it for this video as well:
merge conflicts de-mystified once and for all. If you found this video useful, I'd be extremely
grateful if you were to share it with someone who would benefit. These videos are a labor of love for me, and
seeing comments from folks who unlocked their Git understanding by watching my videos makes it
all worth it. Plus if you're looking for more Git videos,
I've got those too. thanks again, and I'll see you next time. [Bloopers] ... the tricky world of merge co... *messes up the line* *drinks water loudly* *water drinking intensifies*