强制 git stash 覆盖添加的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16606203/
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
Force git stash to overwrite added files
提问by Stefan
I have some files which were untracked in git. I made some changes and wanted to commit them, but realised I had forgotten to check in the unmodified files first. So I stashed the files, then added the unmodified versions.
我有一些未在 git 中跟踪的文件。我做了一些更改并想提交它们,但意识到我忘记先签入未修改的文件。所以我隐藏了文件,然后添加了未修改的版本。
Then when I apply the stash to the repository, I get conflicts due to the files having already been added.
然后,当我将 stash 应用到存储库时,由于已经添加了文件,我会遇到冲突。
How can I apply the stash, and force the versions in the stash to be used in preference to the originals in the repository?
如何应用存储,并强制优先使用存储中的版本而不是存储库中的原始版本?
Thanks
谢谢
回答by tom
Use git checkout
instead of git stash apply
:
使用git checkout
代替git stash apply
:
$ git checkout stash -- .
$ git commit
This will restore all the files in the current directory to their stashed version.
这会将当前目录中的所有文件恢复到它们的隐藏版本。
If there are changes to other files in the working directory that should be kept, here is a less heavy-handed alternative:
如果应该保留工作目录中的其他文件的更改,这里有一个不太笨重的替代方案:
$ git merge --squash --strategy-option=theirs stash
If there are changes in the index, or the merge will touch files with local changes, git will refuse to merge. Individual files can be checked out from the stash using
如果索引有变化,或者合并会触及有本地变化的文件,git会拒绝合并。可以使用从存储中检出单个文件
$ git checkout stash -- <paths...>
or interactively with
或与
$ git checkout -p stash
回答by Hasan TBT
git stash show -p | git apply
git stash show -p | git apply
and then git stash drop
if you want to drop the stashed items.
然后git stash drop
如果你想放下藏起来的物品。
回答by NetEmmanuel
To force git stash pop
run this command
强制git stash pop
运行此命令
git stash show -p | git apply && git stash drop
回答by User12547645
TL;DR:
特尔;博士:
git checkout HEAD path/to/file
git stash apply
Long version:
长版:
You get this error because of the uncommited changes that you want to overwrite. Undo these changes with git checkout HEAD
. You can undo changes to a specific file with git checkout HEAD path/to/file
. After removing the cause of the conflict, you can apply as usual.
由于要覆盖的未提交更改,您会收到此错误。使用 撤消这些更改git checkout HEAD
。您可以使用 撤消对特定文件的更改git checkout HEAD path/to/file
。排除冲突原因后,您可以照常申请。