How to Resolve Merge Conflicts in Git

Git is one of the most popular source-control systems that enables 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. 

The topics covered in this article include:

1. What is Git?

2. Basic Git commands

3. What is a Git merge conflict?

4. Types of Git merge conflicts

5. How to resolve merge conflicts

6. Git commands used to resolve conflicts

7. Demo to resolve Git merge conflict

First, we’ll go over the basics of Git.

What Is Git?

Git is an open-source, distributed version control system (VCS), which has a remote repository on the server side and a local repository on the client side. This means that the file or code is not present in a central server, but there is a copy of the file stored on the client’s computer. 

A distributed version control system enables multiple developers to work in parallel to each other without any code conflicts. Git enables developers to revert and go back to an older version of the code whenever necessary. 

Git helps both developers and non-tech professionals by keeping track of their project files. It makes it easier for multiple individuals to work together, and it plays an extremely significant role in big projects that involve large teams.

git-1

Basic Git Commands

  • git config 
  • git init
  • git add 
  • git diff
  • git commit
  • git reset
  • git status
  • git merge
  • git push
  • git pull

DevOps Engineer Master's Program

Bridge between software developers and operationsExplore Course
DevOps Engineer Master's Program

What Is a Git Merge Conflict?

A 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.

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.

How to Resolve Merge Conflicts in Git

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

  1. The easiest way to resolve a conflicted file is to open it and make any necessary changes
  2. After editing the file, we can use the git add command to stage the new merged content
  3. The final step is to create a new commit with the help of the git commit command
  4. Git will create a new merge commit to finalize the merge

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

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.

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

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 the 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

Enroll in DevOps Engineer Masters Program and learn to work on tools like Docker, Git, Jenkins, Puppet & Nagios.

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. 

Want to Learn More?

As we mentioned, Git is a powerful and powerful tool used by companies across the world. That means that qualified professionals are in high demand. If you want to kick start your career, check out Simplilearn's DevOps Certification Course today. In it, you will gain in-depth knowledge of Git and other popular version control systems, and much more.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

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