Git stash pop- needs merge, unable to refresh index
GitGit StashGit Problem Overview
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.
Git Solutions
Solution 1 - Git
First, check git status
.
As the OP mentions,
> The actual issue was an unresolved merge conflict from the merge, NOT that the stash would cause a merge conflict.
That is where git status
would mention that file as being "both modified
"
> Resolution: Commit the conflicted file.
Solution: in this case, simply add and commit your local file.
You can find a similar situation 4 days ago at the time of writing this answer (March 13th, 2012) with this post: "‘Pull is not possible because you have unmerged files’":
julita@yulys:~/GNOME/baobab/help/C$ git stash pop
help/C/scan-remote.page: needs merge
unable to refresh index
What you did was to fix the merge conflict (editing the right file, and committing it):
See "How do I fix merge conflicts in Git?"
What the blog post's author did was:
julita@yulys:~/GNOME/baobab/help/C$ git reset --hard origin/mallard-documentation
HEAD is now at ff2e1e2 Add more steps for optional information for scanning.
I.e aborting the current merge completely, allowing the git stash pop
to be applied.
See "Aborting a merge in Git".
Those are your two options.
Solution 2 - Git
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)
Solution 3 - Git
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.
Solution 4 - Git
Its much simpler than the accepted answer. You need to:
-
Check
git status
and unmerged paths under it. Fix the conflicts. You can skip this step if you'd rather do it later. -
Add all these files under unmerged paths to index using
git add <filename>
. -
Now do
git stash pop
. If you get any conflicts these will again need to be resolved.
Solution 5 - Git
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
Solution 6 - Git
Well, initially, we should know the root of the error, then the solution will become easy. The reason has already been pointed out by the accepted answer, but it's somehow incomplete (and also the solution).
The problem is, one or more files had conflicts previously, and Git still sees them as unresolved. Although you may already edited those files and resolved the conflicts, but Git is not aware of.
In this case, you should inform Git: "Hey, there are no 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.
Generally, git status
can tell you what Git knows now. If there are some unresolved merge conflicts to Git, 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. This means, unmerged paths are those you should either move into staged or unstaged areas, as Git can work only with these two.
Even in recent versions of Git, when you do a git status
, it tells you the 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 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).
Solution 7 - Git
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.
Solution 8 - Git
git reset
if you don't want to commit these changes.
Solution 9 - Git
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.
Solution 10 - Git
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).
Solution 11 - Git
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.