Why does git say “Pull is not possible because you have unmerged files”?

1.0K    Asked by Ajit yadav in Devops , Asked on Jun 1, 2021

When I try to pull in my project directory in the terminal, I see the following error:

bhamra@bhamra-desktop:~/Sites/branch1$ git pull origin master
U app/config/app.php
U app/config/database.php
U app/routes.php
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm '
as appropriate to mark resolution, or use 'git commit -a'.

Why does git say "Pull is not possible because you have unmerged files", and how can I resolve it?


In case, you are attempting to add one more new commit into your local branch while your working directory is not clean. Hence, Git is refusing to do the pull. Consider the following diagrams to better visualize the scenario:

remote: A <- B <- C <- D 
local: A <- B*
    (*indicates that you have several files which have been modified but not committed.)

There are two options for dealing with this situation. You can either discard the changes in your files or retain them.

Option one: Throw away the changes

You can either use git checkout for each unmerged file, or you can use git reset hard HEAD to reset all files in your branch to HEAD. By the way, HEAD in your local branch is B, without an asterisk. Incase you choose this option, the diagram becomes:

remote: A <- B <- C <- D

local: A <- B

Now when you pull, you can fast-forward your branch with the changes from master. After pulling this code, your branch would look like a master:

local: A <- B <- C <- D

Option two: Retain the changes

In case you desire to keep the changes, you will initially want to resolve any merge conflicts in each of the files. Then you can open each file in your IDE and look for the following symbols:

<<<<<<< HEAD

=======

// the remote's version of the code

>>>>>>>

Git presents you two versions of code i.e. the code contained within the HEAD markers is the version from your current local branch. However, the other version is what is coming from the remote. Once you have chosen a version of the code, you can add each file to your staging area by typing git add.

Now, the final step of pulling is not possible because of unmerged files is to commit your result by typing git commit -m with an appropriate message. At this point, our diagram looks like this:

remote: A <- B <- C <- D

local: A <- B <- C'

Here I have labelled the commit we just made as C' because it varies from the commit C on the remote. Now, if you try to pull you will get a non-fast forward error.

Git cannot play the changes in remote on your branch, because both your branches and the remote have separated from the common ancestor commit B. Moreover, at this point if you want to pull you can either do another git merge, or git rebase your branch on the remote.

Getting Git mastery requires being able to understand and manipulate unidirectional linked lists. I hope this explanation helps you!




Your Answer

Interviews

Parent Categories