Git stash pop- needs merge, unable to refresh index

后端 未结 10 1742
遥遥无期
遥遥无期 2020-12-12 20:00

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         


        
相关标签:
10条回答
  • 2020-12-12 20:23

    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)
    0 讨论(0)
  • 2020-12-12 20:28

    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).

    0 讨论(0)
  • 2020-12-12 20:34

    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.

    0 讨论(0)
  • 2020-12-12 20:35

    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
    
    0 讨论(0)
  • 2020-12-12 20:38

    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.

    0 讨论(0)
  • 2020-12-12 20:39

    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.

    0 讨论(0)
提交回复
热议问题