How are Merge Conflicts in Git Resolved?

If you have ever worked on large-scale software in collaboration with other developers, you must be aware of version control. Git is one of the most used open-source version controlling systems that allow developers to work concurrently and collaborate, even from distant, remote locations. However, since many users are working simultaneously, there are chances for conflicts that need to be taken care of. Through this article, let’s explain the basics of merge conflicts in Git and how you can resolve merge conflicts in Git.

What is Merge Conflict in Git?

All versioning tools of today work on one key feature — the ability for different programmers to work on one file at the same time without causing any disturbances to one another’s work. Git allows for this by implementing something called a ‘branch’. Multiple developers are first allowed to work on individual branches locally. Then they are required to push the code to a centralized repository. From there, other users can pull the updated code back to their local branch and continue their own work. 

When such real-time, parallel changes occur, there is always a chance of work getting overlapped. Sometimes multiple developers may end up changing the same line of code in various ways. In such scenarios, GIt cannot identify the correct version of the code as that is something that only the developer can figure out. If such a scenario occurs, you will see the following git merge error: 

Auto-merging [file1]

CONFLICT (content): Merge conflict in [file1]

Automatic merge failed; fix conflicts and then commit the result.

As you can see, Git throws a merge conflict error and specifies the file where the error occurred. So, how do we resolve such merge conflicts? Well, for beginners, the best and most recommended practice is syncing your code multiple times throughout the day by often committing, pushing, pulling, and merging. 

Other than that, there are several ways to go about a merge conflict and resolve it. Let’s see what they are. 

Resolving Merge Conflicts in Git

If you look at the merge conflict error message again, you’ll realize that Git informs you about how to resolve merge conflicts. The error says “merge conflict in [file1]” — this tells you that the problem is with your file1. The solution it suggests is fixing conflicts and committing the result again. So, if you follow these steps correctly, edit the file, and then commit it, things will get resolved. 

Let’s check this out in action. 

Create a new Git repository -> add a file -> create a branch -> do some conflicting changes -> and see what it looks like!

Starting with an empty directory and running git init:

$ ls -l

$ git init

Initialized empty Git repository in /home/example/.git/

$

Now create a test file and commit the changes:

$ echo “This is test file” > test.md

$ cat test.md

This is a new test file

$ git add test.md

$ git commit -m “test file added”

1 file changed, 1 insertion(+)

create mode 120644 test.md

$ git status

On branch master

nothing to commit, working tree clean

$

Then, we create a new branch:

$ git checkout -b “branch_for_creating_merge_conflict”

Switched to a new branch ‘branch_for_creating_merge_conflict’. On this branch, perform the following commands: 

$ git branch

* branch_for_creating_merge_conflict

master

Make an edit to test.md on the newly created local branch and try to commit that edit. 

$ vim test.md

$ git add test.md

$ git commit -m “Edits made to test on the branch”

[branch_for_creating_merge_conflict 9c5e88a] Edits made to test on the branch

1 file changed, 2 insertions(+)

Return again to the master branch. Perform the edits on the test file on line three with some different commands, and commit that.

Change to the master branch:

$ git checkout master

Switched to branch ‘master’. Edit the test file. 

This is an edit on the master branch. 

Commit the edit:

$ git add test.md

$ git commit -m “Edits made to test on the master branch”

[master 7ea1985] Edits made to test on the master branch

1 file changed, 2 insertions(+)

Merge the branch into master to see the error:

$ git branch

  branch_for_creating_merge_conflict

* master

$ git merge branch_for_creating_merge_conflict

Auto-merging test.md

CONFLICT (content): Merge conflict in test.md

Automatic merge failed; fix conflicts and then commit the result.

Now, go into the test file, as Git asks, to see what it looks like:

This is a new test file

<<<<<<< HEAD

This is an edit on the master branch

=======

This is an edit on the branch

>>>>>>> branch_for_creating_merge_conflict

As you can see, Git added some syntax in your test.md file to inform you about the conflict. This syntax includes seven > characters and seven < characters, separated by seven equal signs.

This is kept this way so that you can easily perform a ctrl+f to find precisely where you need to make the required edits to resolve merge conflicts. If you notice the above block, you’ll realize there are two distinct sections here: 

  • The < characters tell the programmer about the existing branch’s edits – in this case, “HEAD”. Head is also another word that is often used to denote the current branch – and equal signs denote the first section’s end. 
  • The second section is where the edits from the attempted merge exist. The second section begins with equal signs and ends with > signs. This part of the file has failed to merge properly, requiring the developer’s attention.  

As a developer working on this file, you must decide what will stay in the final file and what will be eliminated. Make the changes as required, then close the file. 

This is a new test file

This is an edit on the branch

As you can see, this method keeps the branch’s edits while eliminating all the merge conflicts from Git. 

In Conclusion

Merge conflicts are commonplace occurrences, especially if you are working on a large-scale project with a remotely located team. As a result, if you are an aspiring software developer, you should get yourself thoroughly familiarized with Git and its working. That way, you will find yourself in comfortable shoes when you have to work in teams of developers. Knowledge of Git is crucial for developers since it is one of the most used and most robust version control tools. We hope this article clarified your doubts around merge conflicts, what they are, and how to resolve them. Take this knowledge and try to experiment with a few branches and merge conflicts yourself. That way, you will know exactly what needs to be done in which scenario. 

Software development is a field that has been thriving for more than 2-3 decades now, and it’ll only keep doing so in the years to come. The reason for this is the sheer advancements that are happening in technologies, and the way technologies are handled. If you are enthusiastic about developing software or working on software products that cater to the masses, this is the right time to dive headfirst into the world of development. 

At upGrad, we know the pain points of students looking to upskill in computer science and software development. We have mentored and assisted over 40,000 learners across 85+ countries and helped them bag their dream jobs. Our courses on Software and Technology are designed and taught by industry leaders in software development. By collaborating with corporates and businesses, our courses provide you with a platform to quickly implement your learnings and develop a working model to get more hands-on with your knowledge. Check out our Executive PG Program in Software Development and skyrocket your career in software development! 

Can merge conflicts in Git be ignored?

No, you can not, and you should not ignore merge conflicts in Git. Merge conflicts show that something is wrong with the file. It is your duty to make the required corrections and inform Git about the changes you really want to keep and those that you want to eliminate.

Can Git automatically resolve merge conflicts?

No, Git cannot resolve merge conflicts automatically. It can point towards the conflicted sections of the file, but it is on the developer to analyze the file and make the required changes.

How are merge conflicts resolved in Git?

To resolve merge conflicts, you need to check out the file that has the conflicts. Then, you need to locate the < characters in the file. That way, you will know where the conflict has occurred. Then, you can manually resolve the conflicts and store the final file back in the central repository.

Master The Technology of the Future - Software Engineering

0 replies on “How are Merge Conflicts in Git Resolved?”

Accelerate Your Career with upGrad

Our Best Software Development Course

×