Git stash:“无法应用于脏工作树,请暂存您的更改”
我正在尝试应用之前使用 git stash pop 保存的更改并收到消息:
Cannot apply to a dirty working tree, please stage your changes
有关如何处理该问题的任何建议?
I am trying to apply changes I stashed earlier with git stash pop
and get the message:
Cannot apply to a dirty working tree, please stage your changes
Any suggestion on how to deal with that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
当我必须将隐藏的更改应用于脏工作副本时,例如从隐藏中弹出多个变更集,我使用以下方法:
基本上,它
我想知道为什么
git stash pop
没有-f
(强制)选项code> 其行为应该与上面的一行完全一样。与此同时,您可能想将这一行添加为 git 别名:
感谢 @SamHasler 指出
-3
参数,该参数允许通过 3 路合并直接解决冲突。When I have to apply stashed changes to a dirty working copy, e.g. pop more than one changeset from the stash, I use the following:
Basically it
I wonder why there is no
-f
(force) option forgit stash pop
which should exactly behave like the one-liner above.In the meantime you might want to add this one-liner as a git alias:
Thanks to @SamHasler for pointing out the
-3
parameter which allows to resolve conflicts directly via 3-way merge.我这样做:
然后(可选):
I do it in this way:
and then (optionaly):
您可以通过将所需的存储导出为补丁文件并手动应用它来执行此操作,而无需存储当前的更改。
例如,假设您要将
stash@{0}
应用到脏树:将
stash@{0}
导出为补丁:手动应用更改:
如果第二步失败,您必须编辑
Stash0.patch
文件来修复任何错误,然后再次尝试git apply
。You can do this without having to stash your current changes by exporting the stash you want as a patch file and manually applying it.
For example, say you want to apply
stash@{0}
to a dirty tree:Export
stash@{0}
as a patch:Manually apply the changes:
If the second step fails, you will have to edit the
Stash0.patch
file to fix any errors and then trygit apply
again.使用 git reset 清理工作目录,提交更改,或者,如果您想存储当前更改,请尝试:
这将存储当前更改,然后从存储堆栈中弹出第二个存储。
Either clean your working directory with git reset, commit the changes, or, if you want to stash the current changes, try:
This will stash the current changes, and then pop the second stash from the stash stack.
Mathias 的解决方案绝对是最接近 git stash pop --force (真的,来吧 Git 开发者,让我们已经得到这个选项了!)
但是,如果您只想使用Git命令,你可以:
git commit -a -m "Fixme"
git stash pop
git commit -a --amend
git重置 HEAD~
换句话说,对当前的更改进行提交(我们永远不会推送)。现在你的工作空间已经干净了,可以打开你的储藏室了。现在,提交存储更改作为对之前提交的修改。完成此操作后,您现在可以将两组更改合并到一次提交中(“Fixme”);只需
git reset
(--soft
不是--hard
所以实际上没有丢失任何内容)您签出到“提交之前的一个”,现在您拥有两组完全未提交的更改。编辑
我刚刚意识到它实际上更容易;你可以完全跳过第3步,所以...
git commit -a -m "Fixme"
git stash pop
git reset HEAD~
(Commit当前更改,弹出隐藏的更改,重置第一次提交以使两组更改组合在未提交状态。)
Mathias's solution is definitely the closest to a
git stash pop --force
(and really, c'mon Git devs, let's get this option already!)However, if you want to do the same thing using only Git commands, you can:
git commit -a -m "Fixme"
git stash pop
git commit -a --amend
git reset HEAD~
In other words, make a commit (which we will never push) of your current changes. Now that your workspace is clean, pop your stash. Now, commit the stash changes as an amendment to your previous commit. Having done that you now have both sets of changes combined in a single commit ("Fixme"); just
git reset
(--soft
NOT--hard
so nothing is actually lost) your checkout to "one before that commit", and now you have both sets of changes, completely uncommitted.EDIT
I just realized it's actually even easier; you can completely skip step 3, so ...
git commit -a -m "Fixme"
git stash pop
git reset HEAD~
(Commit current changes, pop off the stashed changes, reset that first commit to get both sets of changes combined in an uncommitted state.)
如果您发现自己像我今天一样处于这种情况,那么这些答案实际上都不起作用。不管我做了多少次 git reset --hard ,都没有任何结果。我的答案(无论如何都不是官方的):
None of these answers actually work if you find yourself in this situation as I did today. Regardless of how many
git reset --hard
's I did, it got me nowhere. My answer (not official by any means was):git reflog --all
我还发现 Mathias Leppich 的解决方案 工作得很好,所以我在我的全局 .gitconfig 中添加了它的别名,
现在我只需输入
对我来说非常有用的内容即可。
(你对这个长别名的理解可能会有所不同。但我喜欢 bash 完成时的冗长。)
I also found Mathias Leppich's solution to work great so I added an alias for it to my global .gitconfig
Now I can just type
which works great for me.
(Your mileage may vary on this long alias name. But I like a dose of verbosity when it comes with bash completion.)
您可以通过执行 git add 来暂存您所做的任何更改,从而将存储应用到“脏”树,从而清理树。然后您可以 git stash pop 并应用隐藏的更改,没问题。
You can apply a stash to a "dirty" tree by doing a
git add
to stage any changes you've made, thus cleaning up the tree. Then you cangit stash pop
and apply the stashed changes, no problem.您有已修改但未提交的文件。或者:
或者,如果您想保存更改:
You have files that have been modified but not committed. Either:
or, if you want to save your changes:
我遇到了同样的问题,但 git 的更改文件为零。原来我有一个 index.lock 文件。删除它解决了问题。
I had the same problem but git had zero changed files. Turns out I had a index.lock file that was lying around. Deleting it solved the problem.
我无法让其中大部分发挥作用;由于某种原因,它总是认为我对文件进行了本地更改。我无法应用存储,补丁也不会应用,
checkout
和reset --hard
失败。最终起作用的是将存储保存为使用 git stashbranchtempbranchname 的分支,然后进行正常的分支合并:git checkout master 和 git merge tempbranchname代码>.来自 http://git-scm.com/book/en/Git-Tools-隐藏:
I was unable to get most of these to work; for some reason it always thinks I have local changes to a file. I can't apply a stash, patches won't apply,
checkout
andreset --hard
fail. What finally worked was saving the stash as a branch withgit stash branch tempbranchname
, and then doing a normal branch merge:git checkout master
andgit merge tempbranchname
.From http://git-scm.com/book/en/Git-Tools-Stashing :
这就是我对这个问题的看法,对我来说没有问题。
当然,它部分地求助于 git 命令系列之外的命令。
That's my take on the problem that worked for me without problems.
Of course it partially resorts to a command outside the
git
commands family.