Handling Merge Conflicts

Bharat Khiani
A free video tutorial from Bharat Khiani
Udemy Instructor, Freelance developer
4.0 instructor rating • 2 courses • 7,142 students

Lecture description

  • Reasons for merge conflicts and how to resolve them manually to successfully merge. 

  • How to abort merges in merge conflicts

Learn more from the full course

Git Beginner

A comprehensive course on git and github for beginners covering commands on the linux terminal.

05:10:41 of on-demand video • Updated June 2020

  • Work with GIT commands on the terminal
  • Record changes to your source code files in commits
  • Use branches, reset, revert and other well-known features in GIT
  • Publish your projects on github and collaborate with other developers
English When you start merging branches regularly, you might come across merge conflicts. This is usually the case when the type of merge is a recursive merge. A Merge conflict occurs... when the same file changes across branches in our project and we try to merge these branches. In this lecture I shall demonstrate how we can resolve merge conflicts, or abort the merge altogether. Lets try to understand this with an example. Let's create a new project We will add a new file with one line of data and create a commit. Lets create a new branch called feature1 and checkout to it. On this branch... I will make changes to file1 and stage and commit the changes. We will now checkout to the master branch. The master branch's version of file1.txt does not have a second line. If we modify the file1, add a second line to it and commit on master, then the changes on feature1 will differ from the changes made to the file, on the master branch. Let's look at all the commits in all the branches. The output shows that the branches have unique commits of their own. If we merge now, we shall have a recursive merge. We are also aware that we have modified the same file across the branches. So we shall get into a merge conflict. So we shall get into a merge conflict. Let us first assure that we are checked out to the destination branch of the merge We are currently on the master branch. Let's begin the merge. The output here, tells us that the merge failed and that we would have to resolve the merge conflicts... manually or abort the merge altogether. The 'git status' command describes the merge conflict and suggests how to fix it. So 'git status' informs us that we could either abort the merge. Or, we could resolve the differences in the file and stage and commit to complete the merge. It also lists the files that caused the merge conflict. Resolving a merge conflict manually involves opening the files that caused the merge conflict, fixing lines of code, staging and committing the files. Aborting the merge will take the repository to a state we had, just before we issued the 'git merge' command. We see a message here, both modified for file1; which means file1 contains modifications from both branches. Let us open file1.txt in our text editor and fix the merge What I see here are special markers indicating sections of data from the branches we were trying to merge. The line below the <<<<< sign is from the master branch. I could identify it because we are currently checked out to the master branch and HEAD here indicates the master branch. The line below the ====== sign is one from feature1 branch. I choose to keep data from both branches and get rid of these markers using the 'Cntrl + K' shortcut. After removing the markers, I have consolidated the changes from both branches in this file. I can now... save the file and exit using 'Cntrl + O' and 'Cntrl + X' shortcuts. OK so I have resolved the merge conflict. To indicate to Git, that it has been resolved... I need to stage and commit. Let us look at the entire repository using 'git log' with options. I see that the merge succeeded. So that's how we resolve a merge conflicts when in a merge conflict. We could also choose to undo the merge. That is, to go back to the State of the repository just before we issued the 'git merge' command. To see that, let us create two unique commits on each branch, modifying the same file. I am currently on the master branch. So I have different data for the same file in both branches. If I merge feature1 into master, I should get a merge conflict. Let us first checkout to the master branch. Let us try to merge. We got into a merge conflict. Let us abort the merge now. The merge cancelled itself. 'git status' should come clean now. That is indeed the case. So that's about it - for resolving merge conflicts or aborting merges when in a conflict. There are workflows in Git, that ensure that you don't get into merge conflicts at all. However if you ever get into one, you know how to abort or resolve the merge. In the next lecture, I shall demonstrate how we can force git to make a new commit even on a fast forward merge. See you in the next lecture. Thank you ! :)