Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Development USbreadcumb forward arrow iconHow are Merge Conflicts in Git Resolved?

How are Merge Conflicts in Git Resolved?

Last updated:
16th Sep, 2021
Views
Read Time
7 Mins
share image icon
In this article
Chevron in toc
View All
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]

Ads of upGrad blog

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. 

Ads of upGrad blog

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! 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.
Get Free Consultation

Selectcaret down icon
Select Area of interestcaret down icon
Select Work Experiencecaret down icon
By clicking 'Submit' you Agree to  
UpGrad's Terms & Conditions

Our Best Software Development Course

Frequently Asked Questions (FAQs)

1Can 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.

2Can 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.

3How 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.

Explore Free Courses

Suggested Blogs

Runtime Polymorphism in Java with Examples
41567
Polymorphism is a technique wherein a single action can be performed in two different ways. The term polymorphism is derived from two Greek words, 
Read More

by Pavan Vadapalli

19 Jun 2024

Top 19 Java 8 Interview Questions (2023)
6288
Java 8: What Is It? Let’s conduct a quick refresher and define what Java 8 is before we go into the questions. To increase the efficiency with
Read More

by Pavan Vadapalli

27 Feb 2024

Top 10 DJango Project Ideas &#038; Topics
13567
What is the Django Project? Django is a popular Python-based, free, and open-source web framework. It follows an MTV (model–template–views) pattern i
Read More

by Pavan Vadapalli

29 Nov 2023

Most Asked AWS Interview Questions &#038; Answers [For Freshers &#038; Experienced]
5835
The fast-moving world laced with technology has created a convenient environment for companies to provide better services to their clients. Cloud comp
Read More

by upGrad

07 Sep 2023

22 Must-Know Agile Methodology Interview Questions &#038; Answers in US [2024]
5447
Agile methodology interview questions can sometimes be challenging to solve. Studying and preparing well is the most vital factor to ace an interview
Read More

by Pavan Vadapalli

13 Apr 2023

12 Interesting Computer Science Project Ideas &#038; Topics For Beginners [US 2023]
11846
Computer science is an ever-evolving field with various topics and project ideas for computer science. It can be quite overwhelming, especially for be
Read More

by Pavan Vadapalli

23 Mar 2023

Begin your Crypto Currency Journey from the Scratch
5507
Cryptocurrency is the emerging form of virtual currency, which is undoubtedly also the talk of the hour, perceiving the massive amount of attention it
Read More

by Pavan Vadapalli

23 Mar 2023

Complete SQL Tutorial for Beginners in 2024
5644
SQL (Structured Query Language) has been around for decades and is a powerful language used to manage and manipulate data. If you’ve wanted to learn S
Read More

by Pavan Vadapalli

22 Mar 2023

Complete SQL Tutorial for Beginners in 2024
5079
SQL (Structured Query Language) has been around for decades and is a powerful language used to manage and manipulate data. If you’ve wanted to learn S
Read More

by Pavan Vadapalli

22 Mar 2023

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon