How to Resolve Merge Conflicts in Git?

Git is one of the most popular source-control systems that enable software development professionals in all industries, enabling multiple team members to work concurrently on projects. Since many users are simultaneously working from different places on the same file, however, you may end up with a merge conflict. This article explains the basics of Git merge conflicts and one of the advanced operations of Git: resolving a Git merge conflict. 

What is a Git Merge Conflict?

A git merge conflict is an event that takes place when Git is unable to automatically resolve differences in code between two commits. Git can merge the changes automatically only if the commits are on different lines or branches.

The following is an example of how a Git merge conflict works:

pull-push.

Let’s assume there are two developers: Developer A and Developer B. Both of them pull the same code file from the remote repository and try to make various amendments in that file. After making the changes, Developer A pushes the file back to the remote repository from his local repository. Now, when Developer B tries to push that file after making the changes from his end, he is unable to do so, as the file has already been changed in the remote repository.

To prevent such conflicts, developers work in separate isolated branches. The Git merge command combines separate branches and resolves any conflicting edits.

Now that we have gone through the basics of the Git merge conflict, let’s look at the various types of conflicts next.

Get All Your Career Growth Questions Answered!

DevOps Engineer Masters ProgramExplore Program
Get All Your Career Growth Questions Answered!

How to Resolve Merge Conflicts in Git?

There are a few steps that could reduce the steps needed to resolve merge conflicts in Git.

Step 1: The easiest way to resolve a conflicted file is to open it and make any necessary changes.

Step 2: After editing the file, we can use the git add a command to stage the new merged content.

Step 3: The final step is to create a new commit with the help of the git commit command.

Step 4: Git will create a new merge commit to finalize the merge.

Learn from Experienced Industry Experts!

DevOps Engineer Masters ProgramExplore Program
Learn from Experienced Industry Experts!

Let us now look into the Git commands that may play a significant role in resolving conflicts.

Git Commands to Resolve Conflicts

1. git log --merge 

The git log --merge command helps to produce the list of commits that are causing the conflict.

2. git diff 

The git diff command helps to identify the differences between the states repositories or files.

3. git checkout 

The git checkout command is used to undo the changes made to the file, or for changing branches.

4. git reset --mixed 

The git reset --mixed command is used to undo changes to the working directory and staging area.

5. git merge --abort

The git merge --abort command helps in exiting the merge process and returning back to the state before the merging began.

6. git reset

The git reset command is used at the time of merge conflict to reset the conflicted files to their original state.

Get the Coding Skills You Need to Succeed

Full Stack Developer - MERN StackExplore Program
Get the Coding Skills You Need to Succeed

Types of Git Merge Conflicts 

There are two points when a merge can enter a conflicted state:

1. Starting the Merge Process 

If there are changes in the working directory’s stage area for the current project, merging won’t start. 

In this case, conflicts happen due to pending changes that need to be stabilized using different Git commands.

2. During the Merge Process

The failure during the merge process indicates that there is a conflict between the local branch and the branch being merged.

In this case, Git resolves as much as possible, but there are things that have to be resolved manually in the conflicted files.

We will now go over resolving merge conflicts in Git.

Demo: Resolving Git Merge Conflicts

First, initialize two repositories: 

git init A

git init B

remote-address.

Add the remote address in the A repository:

git remote add origin *address*

next

The next step is to pull all the changes in the central repository to the local repository.

git pull origin master

follow

Follow the same process to add the origin in the B repository.

git remote add origin *address*

the-pull

The pull command is executed again to retrieve all the content from the remote repository and move it to the local repository.

git pull origin master

both

Both of these repositories represent two different repositories of two different developers.

Earn the Most Coveted DevOps Certification!

DevOps Engineer Masters ProgramExplore Program
Earn the Most Coveted DevOps Certification!

Let's get back to the A repository.

cd ../A

In the A repository, a readme file is opened in order to make various changes.

vi README.md

make

Make the necessary changes in the file, and then save it.

The git status command is then executed in order to see the reflected changes. 

git status

next-st

The next step is to add these changes to the staging area and commit them.

git add.

git commit -m *commit message*

after

Earn the Most Coveted DevOps Certification!

DevOps Engineer Masters ProgramExplore Program
Earn the Most Coveted DevOps Certification!

After the commit is finished, the changed file is pushed to the remote repository.

git push origin master

Now, return to the B repository. 

cd B

open-a

Open a readme file 

vi README.md

make-changes

Make changes to the file, save it, and close it. After that, add the changed file and commit it.

git add.

git commit -m *commit message*

The next step is to push the file to the remote repository.

git push

an-error

An error is shown, meaning that the updates are rejected.

Next, we need to execute:

git -- rebase origin master

currently

Currently, there are visible conflicts that need to be resolved manually.

If you want to skip this commit, you can type git rebase --skip, or if you want to abort this rebase, you can type git rebase --abort.

After managing this conflict manually, we will open the merge tool.

git mergetool

input

After we input this command, all of the files will be processed.

processes

These are all the processes and the modifications done in the file.

You can see three different files there, and you can see everything that was added or removed. 

After scrolling, you can verify where exactly the conflict happened.

decide

You can then decide if you want to continue with this particular file or not. I will proceed with removing that line.

manual

Manual modifications have allowed us to resolve file conflicts. Save the file and close the final file.

will-run

Next, we will run:

git rebase --continue

conflict.

Now, when the conflict is resolved, we must be able to push the file to the remote repository.

/now-check

You can now check the commits in your remote repository.

conclusion

Gain Hands-on Experience Through Real Projects

Post Graduate Program In Cloud ComputingExplore Program
Gain Hands-on Experience Through Real Projects

Conclusion

We hope that this comprehensive tutorial will help you with Git merge conflicts. We went over the basics of the merge conflicts, including the types of merge conflicts and possible explanations for their occurrence. We also provided a detailed example through a step-by-step demo. In the demo, we saw how we can manually resolve merge conflicts. 

If you are looking to enhance your DevOps skills further, we would highly recommend you to check Simplilearn's Post Graduate Program in DevOps. This can help you gain the right skills and make you job-ready in no time.

If you have any questions or queries, feel free to post them in the comments section below. Our team will get back to you at the earliest.

FAQs

1. What is a merge conflict in Git?

A merge conflict in Git occurs when two or more developers edit the same file lines simultaneously, and Git finds difficulty in reconciling the changes.

2. How can I identify a merge conflict in Git?

Git notifies of a merge conflict after an attempted git merge, marking the affected file with conflict markers.

3. How can I resolve merge conflicts in Git?

Merge conflicts in Git can be resolved using merge tools or manually by editing the conflicting code and committing the resolved file.

4. What are Git merge tools?

Git merge tools are utilities, like KDiff3 or Meld, that provide a graphical interface to manage and resolve Git merge conflicts.

5. What does 'git merge --abort' do?

'git merge --abort' command reverts the merge action, restoring the project to its state before the merge attempt.

6. Can I prevent merge conflicts in Git?

While not entirely avoidable, frequent and smaller commits, proper communication and using 'git pull' before pushing changes can reduce the likelihood of merge conflicts.

About the Author

Sana AfreenSana Afreen

Sana Afreen is a Senior Research Analyst at Simplilearn and works on several latest technologies. She holds a degree in B. Tech Computer Science. She has also achieved certification in Advanced SEO. Sana likes to explore new places for their cultures, traditions, and cuisines.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.