When I try to pull in my project directory in the terminal, I see the following error:
harsukh@harsukh-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?
This question is related to
git
git-pull
merge-conflict-resolution
git-fetch
Just run this command:
git reset --hard
Steps to follow :
step-1 : git reset --hard HEAD (if you want to reset it to head)
step-2 : git checkout Master
step-3 : git branch -D <branch Name>
(Remote Branch name where you want to get pull)
step-4 : git checkout <branch name>
step-5 : git pull. (now you will not get any
error)
Thanks, Sarbasish
You are attempting to add one more new commits into your local branch while your working directory is not clean. As a result, 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, you branch would look like 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 a mastery of Git requires being able to understand and manipulate uni-directional linked lists. I hope this explanation will get you thinking in the right direction about using Git.
There was same issue with me
In my case, steps are as below-
U project/app/pages/file1/file.ts
U project/www/assets/file1/file-name.html
$ git pull origin master
$ git status
Here is the message which It appeared-
and have 2 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: project/app/pages/file1/file.ts
both modified: project/www/assets/file1/file-name.html
$ git add project/app/pages/file1/file.ts
project/www/assets/file1/file-name.html
$ git commit -am "resolved conflict of the app."
$ git push origin master
You have some files locally that need to be merged before you can pull. You could checkout the files and then pull to overwrite your local files.
git checkout app/config/app.php app/config/database.php app/routes.php
git pull origin master
When a merge conflict occurs , you can open individual file. You will get "<<<<<<< or >>>>>>>" symbols. These refer to your changes and the changes present on remote. You can manually edit the part that is requires. after that save the file and then do : git add
The merge conflicts will be resolved.
Theres a simple solution to it. But for that you will first need to learn the following
vimdiff
To remove conficts, you can use
git mergetool
The above command basically opens local file, mixed file, remote file (3 files in total), for each conflicted file. The local & remote files are just for your reference, and using them you can choose what to include (or not) in the mixed file. And just save and quit the file.
If you dont want to merge the changes and still want to update your local then run:
git reset --hard HEAD
This will reset your local with HEAD and then pull your remote using git pull.
If you've already committed your merge locally (but haven't pushed to remote yet), and want to revert it as well:
git reset --hard HEAD~1
If you want to pull down a remote branch to run locally (say for reviewing or testing purposes), and when you $ git pull
you get local merge conflicts:
$ git checkout REMOTE-BRANCH
$ git pull (you get local merge conflicts)
$ git reset --hard HEAD (discards local conflicts, and resets to remote branch HEAD)
$ git pull (now get remote branch updates without local conflicts)
If you dont want any of your local branch changes i think this is the best approach
git clean -df
git reset --hard
git checkout REMOTE_BRANCH_NAME
git pull origin REMOTE_BRANCH_NAME
Source: Stackoverflow.com