Back

Explore Courses Blog Tutorials Interview Questions
+6 votes
2 views
in DevOps and Agile by (19.7k points)

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 <file>'

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?

closed

1 Answer

+9 votes
by (62.9k points)
selected by
 
Best answer

You are attempting to add one more new commits 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. If 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, your branch would look like a master:

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

Option two: Retain the changes 

If you want to keep the changes, you will first want to resolve any merge conflicts in each of the files. You can open each file in your IDE and look for the following symbols:

<<<<<<< HEAD 

// your version of the code 

======= 

// the remote's version of the code 

>>>>>>>

Git is presenting you with two versions of code. The code contained within the HEAD markers is the version from your current local branch. The other version is what is coming from the remote. Once you have chosen a version of the code (and removed the other code along with the markers), you can add each file to your staging area by typing git add. The final step 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 is different 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 branch and the remote have diverged from the common ancestor commit B. At this point if you want to pull you can either do another git merge, or git rebase your branch on the remote.

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

Also, if you wish to get in-depth knowledge about it, you should also read the Git Tutorial and enroll in Git Training.

by (29.3k points)
Nice explanation.

Browse Categories

...