不能弹出 git 存储,“您对以下文件的本地更改将被合并覆盖”

所以我做了很多修改,还有一些未跟踪的文件。我需要调整一些东西,所以我使用了 git stash -u,修改了一些东西,提交了那些更改,推送它们,然后尝试使用 git stash pop

因为我修改了我藏起来的几个文件,我得到了以下信息:

error: Your local changes to the following files would be overwritten by merge:
file_1.py
file_2.py
Please, commit your changes or stash them before you can merge.
Aborting

这看起来很奇怪,我已经提交了所有新的更改,当我运行命令时,我的签出是干净的。

看起来 git stash pop操作解除了我一半的修改和未跟踪的文件,但是如果我再次尝试和 git stash pop我会得到如下输出:

some_file.html already exists, no checkout
some_other_file.html already exists, no checkout
yet_another_file.html already exists, no checkout
Could not restore untracked files from stash

git stash show仍然显示了我隐藏的更改的列表,但是我不知道我现在要做什么。

我怎样才能摆脱困境?

47977 次浏览

The stash that was made with -u needs to have the untracked files cleaned away before being apply-ed (and pop is just apply+drop).

Out of general paranoia I'd mv the untracked files somewhere safe, then git stash apply, check everything carefully, and git stash drop once I'm sure I have it all correct. :-)

I got around this, I think it must have been some kind of bug, as my working directory was clean and up to date.

I ran git checkout . and after that git stash apply worked fine, I got everything back no problems at all. I'd be interested to work out what actually caused it to fail though.

For those who do have un-committed work, and want to pop their stash without losing that work, here is a way (with thanks to @iFreilicht):

  1. Temporarily stage any uncommitted changes:

     git add -u .
    
  2. Now you can apply your stash without git complaining (hopefully):

     git stash pop
    
  3. Now unstage everything, but leave the files as they are now:

     git reset
    

If step 2 couldn't patch cleanly due to conflicting changes, then you will need to resolve the conflicts manually. git diff should help you find them. git mergetool might help by opening your editor with before and current files.

None of these solutions worked for me. I was using git stash index command to restore a specific stash id. So, I ended up doing a commit of my local changes to local repo. Then git stash index worked for me. And finally I rolled back my commit using git reset (with keep changes). Problem solved.