[git] Git stash pop- needs merge, unable to refresh index

I can't pop my stash because I merged a branch which apparently conflicts with my stash and now my stash is seemingly unable to be popped.

app.coffee: needs merge
unable to refresh index

Anyone know how to resolve this?

FIXED!

Turns out the actual issue was an unresolved merge conflict from the merge, NOT that the stash would cause a merge conflict.

Resolution: Commit the conflicted file.

This question is related to git git-stash

The answer is


I was facing the same issue because i have done some changes in my develop branch and then want to go to the profile branch. so i have stash the changes by

git stash

then in profile branch i have also done some changes and then want to come back again to the develop so i have to stash the changes again by

 git stash

but when i come to develop branch and tried to git the stash changes by

git stash apply

so i was getting error need merge

to solve this issue first i have to check the stash list by

git stash list

so it shows the list of stashes in my case there were 2 stashes the name of the stashes are displaying like this stash@{0},stash@{1}

I have need changes from stash@{1} so when i try to get it by this command

git stash apply stash@{1}

so was getting error needs merge

so now to solve this issue check the status of your files

git status

so it was giving error that "both modified" so to solve this run

git add .

it will add the missing modified files now again check the status

git status 

so now there is no error now can apply stash

git stash apply stash@{1}

you can do this process for any number of stash files.


If anyone is having this issue outside of a merge/conflict/action, then it could be the git lock file for your project causing the issue.

git reset
     fatal: Unable to create '/PATH_TO_PROJECT/.git/index.lock': File exists.
rm -f /PATH_TO_PROJECT/.git/index.lock
git reset
git stash pop

I was having this issue, then resolving the conflict and commiting, and doing git stash pop again was restoring the same stash again (causing the same conflict :-( ).

What I had to do (WARNING: back up your stash first) is git stash drop to get rid of it.


git reset if you don't want to commit these changes.


Well, initially, we should know what caused the error to happen, then the solution will be easy. The reason have already been pointed out by the accepted answer, but it is somehow incomplete (also the solution).

The problem is, one or more files had conflict(s) previously, but Git sees them as unresolved. Yes, you might already edited those files and resolved the conflicts, but Git does not know about that. You should tell Git "Hey, there are no more conflicts from the previous merge!". Note that, the merge is not necessarily caused by a git merge, but also by a git stash pop, for example.

Remember, git status can tell you what Git knows now. If there are some unresolved merge conflicts to Git, then it is shown in a separated Unmerged paths section, with the files marked as both modified (always?). If you have noticed, this section is between two staged and unstaged sections. From this, I personally understand that, "Unmerged paths are those you should either move into staged or unstaged areas, as Git can work only with these two areas".

So, to tell Git the conflicts have been resolved, you should either move these changes to staged or unstaged areas. In recent versions of Git, when you do a git status, it tells you how (woah! You should ask yourself how you haven't seen this yet?):

$ git status
...
Unmerged paths:
  (use "git restore --staged <file>..." to unstage)
  (use "git add <file>..." to mark resolution)
        both modified:   path/to/file.txt
...

So, to stage it (and maybe commit it):

$ git add path/to/file.txt

And to make it unstaged (e.g. you don't want to commit it now):

$ git restore --staged path/to/file.txt

Note: Forgetting to write --staged option possibly could spawn a super-hungry dragon to eat your past two days, in the case of not using a good text-editor or IDE.

Note: While git restore command is experimental yet, it should be stable enough to be used (thanks to a comment by @VonC, refer to it for more details on that).


Here's how I solved the issue:

  • git status (see a mix of files from a previous stash, pull, stash pop, and continued work.)
  • git stash (see the needs merge issue)
  • git add . (add the files so my work locally resolves my own merged)
  • git stash (no error)
  • git pull (no error)
  • git stash pop (no error and continue working)

You need to add app.coffee to staging.

Do git add app.coffee and then you will be able to apply your stash (after that commit and push).


Its much simpler than the accepted answer. You need to:

  1. Check git status and unmerged paths under it. Fix the conflicts. You can skip this step if you'd rather do it later.

  2. Add all these files under unmerged paths to index using git add <filename>.

  3. Now do git stash pop. If you get any conflicts these will again need to be resolved.


The stash has already been applied to other files.

It is only app.coffee that you have to merge manually. Afterwards just run

git reset

to unstage the changes and keep on hacking.


I have found that the best solution is to branch off your stash and do a resolution afterwards.

git stash branch <branch-name>

if you drop of clear your stash, you may lose your changes and you will have to recur to the reflog.