One of the common errors when working in a collaborative Git project is “git pull not possible you have unmerged files”.
In this tutorial, we will learn what this error is, why it happens, and how we can resolve it.
What Is this Error?
The “git pull is not possible because you have unmerged files” error occurs when you have unresolved conflicts in the local Git repository. This means that Git is unable to perform a “pull” operation until we fix the conflicting changes.
Let us say you have a public repository that you wish to contribute. You clone the repository on to your local machine. Next, you visit the “Issues” page, pick an issue that you wish to fix or a feature that you wish to add.
Let us say another developer on the other end of the world is trying to fix the same issue that you are fixing or is trying to add the same feature as you. However, that developer pushes the changes before you.
When you try to pull the latest change from the repo, Git cannot determine whether to overwrite your changes or the latest ones from the repo. This is where the pull fails with the unmerged files error.
How to Resolve It
There are various ways that you can use to resolve this error. Some are destructive and will completely remove all the changes that you made and replace them with the new repo version while the other involves fixing the conflicting files.
Solution 1: Identify the Conflicting Files
The first step before attempting to fix any issue is to get a handle on what files are causing the conflict.
You can do this by running the “git status” command:
Git lists the files with conflicts under the “Unmerged paths” section.
Step: Manually Resolve It
You can then open the conflicting files using a text editor or a suitable merge tool like Diff Merge to compare and manually fix the conflicts.
You’ll see the conflict markers in the file like <<<<<<<, =======, and >>>>>>> which indicate the conflicting sections.
For example, you might have a conflict that looks like this:
These are my changes in the current branch
=======
A conflicting change in the incoming branch.
From here, you can choose whether to keep the changes (HEAD section), the incoming changes (incoming-branch section), or a modified version that combines both.
Once done, add the new non-conflicting changes, commit, and push to remote repo.
Solution 2: Recommended
The next step is very suitable in a wide array of cases and provides a gentler and graceful approach to fixing the conflicting issues.
This involves three main steps:
Step 1: Ditch Your Changes
The first step is to put your changes out of the way. This gives a clean repo since your last commit. The good thing is that your changes do not disappear. Instead, Git puts them aside and allows you to retrieve them later when necessary.
You can do this by running the “git stash” command:
The command allows us to temporarily save the changes in the current directory without actually committing them.
Step 2: Pull the Latest Changes
Once we set aside our changes, we can proceed and pull the latest changes from the repo. Run the following “git pull” command:
Step 3: Apply Stash
Finally, we can apply the stashed changes back to the working directory using the “git stash apply” or “git stash pop”.
The “git stash apply” command applies the stash but keeps it in the stash list.
The “git stash pop” command applies the stash and removes it from the list.
Solution 3: Let Git Take Care of It
Git is more powerful than we can think. Instead of manually going all through this hustle, we can just let Git take care of the conflicting changes using the following command:
Using this command, we can keep the local changes safe while keeping the branch up-to-date with the remote changes.
Conclusion
This tutorial explores the “git pull is not possible because you have unmerged files” error when running a “git pull” command. We explored what it means, why it occurs, and how to resolve it.