Git Unmerged Files
While working with Git in a team environment on a complex project, there are many possibilities that some common issues take place anytime. Among them, merging and conflicts are common parts of these issues.
Resolving conflicts in other version control systems is very hectic because it is costly and time-consuming. Whereas in Git, it is super easy to deal with these situations.
Mostly, Git automatically solves how to merge new changes and makes developers work very easy to manage these tasks within no time.
Error in Git Merging
The error usually occurs when files with unresolved conflicts are in the working directory. Conflicts occur when developers work on a huge project within the team structure, and some developers work on the same file and changes are made in conflicting ways.
If two people are operating the same file, most likely Git can figure it out by itself. But sometimes Git can’t do it because sometimes the same changes are made on the same file by two different developers.
And in this situation, Git has no idea what to do. Here we have to manage the situation on our own by opening the file in Notepad or any Visual Studio Code tool to manually merge the conflicting lines in the code file.
git status
Firstly, we will use the command git status
to know where the merge conflict is. Here Git will spot the places that were edited in conflicting ways by specially mentioning the category of the unmerged paths
as follows:
$ git status
# On branch contact-form
# You have unmerged paths.
# (fix conflicts and run "git commit")
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: contact.html
#
Conflicts Solved
As now the conflict is detected, we will change this conflicted desired file to its proper form. This can be accomplished in two different techniques.
- In this method, we will open the conflicted file in the editor like in Notepad or any Visual Studio Code tool, find the conflict markers, and make some modifications as per our needs. This modification will make the file look as we want it to look.
- In the other method, we can ask Git to do the desired task with the help of edited versions, known as
ours
ortheirs
as follows:
git checkout --ours path/to/conflict-file.css
After solving the conflict successfully, we have to do two more steps for the final result.
- Perform
git add <filepath>
, marking each conflicted file as solved. - Finalize the process with the command
git commit
to commit the resolution just as we would like to commit any other change with this command.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn