无法弹出 git stash,“您对以下文件的本地更改将被合并覆盖”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19937580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:17:28  来源:igfitidea点击:

Can't pop git stash, 'Your local changes to the following files would be overwritten by merge'

gitgit-stash

提问by fredley

So I had a load of changes and some untracked files. I needed to tweak something, so I used git stash -u, modified a couple of things, committed those changes, pushed them, and then tried to git stash pop.

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

Because I'd modified a couple of files that I'd stashed, I got the following message:

因为我修改了几个我藏匿的文件,所以我收到以下消息:

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

This seems odd, I had committed all new changes, my checkout was clean when I ran the command.

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

It seems the git stash popoperation un-stashed half of my changes and the untracked files, but if I try and git stash popagain I get output like:

似乎该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 showstill shows a list of my stashed changes, but I'm at a loss as to what I do now.

git stash show仍然显示了我隐藏的更改列表,但我对我现在所做的一无所知。

How can I get myself unstuck?

我怎样才能让自己解脱?

采纳答案by fredley

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 applyworked fine, I got everything back no problems at all. I'd be interested to work out what actually caused it to fail though.

我跑了git checkout .,然后git stash apply工作正常,我把一切都找回来了,没有任何问题。不过,我很想弄清楚究竟是什么导致了它失败。

回答by joeytwiddle

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

对于那些确实有未提交的工作,并希望在不丢失工作的情况下弹出他们的藏匿处的人,这里有一种方法(感谢@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
    
  1. 临时暂存任何未提交的更改:

    git add -u .
    
  2. 现在你可以在没有 git 抱怨的情况下应用你的 stash(希望如此):

    git stash pop
    
  3. 现在取消暂存所有内容,但保留文件原样:

    git reset
    

回答by torek

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

使用 stash-u需要在被apply-ed之前清除未跟踪的文件(并且pop只是apply+ drop)。

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

出于普遍的偏执,我将mv未跟踪的文件放在安全的地方,然后git stash apply仔细检查所有内容,git stash drop一旦我确定我的所有内容都是正确的。:-)

回答by pkso

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.

这些解决方案都不适合我。我正在使用 git stash index 命令来恢复特定的 stash id。所以,我最终将我的本地更改提交到本地存储库。然后 git stash index 对我有用。最后我使用 git reset (保留更改)回滚了我的提交。问题解决了。