View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

How are Merge Conflicts in Git Resolved?

By Pavan Vadapalli

Updated on Sep 29, 2022 | 7 min read | 5.3k views

Share:

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! 

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Frequently Asked Questions (FAQs)

1. Can merge conflicts in Git be ignored?

2. Can Git automatically resolve merge conflicts?

3. How are merge conflicts resolved in Git?

Pavan Vadapalli

900 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad KnowledgeHut

upGrad KnowledgeHut

Angular Training

Hone Skills with Live Projects

Certification

13+ Hrs Instructor-Led Sessions

upGrad

upGrad KnowledgeHut

AI-Driven Full-Stack Development

Job-Linked Program

Bootcamp

36 Weeks