Git stash:“不能应用于肮脏的工作树,请暂存您的更改”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1360712/
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 06:57:40  来源:igfitidea点击:

Git stash: "Cannot apply to a dirty working tree, please stage your changes"

gitgit-stash

提问by avernet

I am trying to apply changes I stashed earlier with git stash popand get the message:

我正在尝试应用我之前隐藏的更改git stash pop并收到消息:

Cannot apply to a dirty working tree, please stage your changes

Any suggestion on how to deal with that?

关于如何处理的任何建议?

回答by muhqu

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:

当我必须将隐藏的更改应用于脏工作副本时,例如从存储中弹出多个变更集,我使用以下内容:

$ git stash show -p | git apply -3 && git stash drop

Basically it

基本上它

  1. creates a patch
  2. pipes that to the apply command
  3. if there are any conflicts they will need to be resolved via 3-way merge
  4. if apply (or merge) succeeded it drops the just applied stash item...
  1. 创建补丁
  2. 管道到应用命令
  3. 如果有任何冲突,它们将需要通过 3 路合并解决
  4. 如果应用(或合并)成功,它会丢弃刚刚应用的存储项...

I wonder why there is no -f(force) option for git stash popwhich should exactly behave like the one-liner above.

我想知道为什么没有-f(force) 选项git stash pop应该与上面的单行完全一样。

In the meantime you might want to add this one-liner as a git alias:

同时,您可能希望将此单行添加为 git 别名:

$ git config --global --replace-all alias.unstash \
   '!git stash show -p | git apply -3 && git stash drop'
$ git unstash


Thanks to @SamHasler for pointing out the -3parameter which allows to resolve conflicts directly via 3-way merge.

感谢@SamHasler 指出-3允许通过三向合并直接解决冲突的参数。

回答by Sergii Mostovyi

I do it in this way:

我是这样做的:

git add -A
git stash apply

and then (optionaly):

然后(可选):

git reset

回答by Ishan

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:

例如,假设您要将 stash@{0} 应用于一棵脏树:

  1. Export stash@{0} as a patch:

    git stash show -p stash@{0} > Stash0.patch

  2. Manually apply the changes:

    git apply Stash0.patch

  1. 将 stash@{0} 导出为补丁:

    git stash show -p stash@{0} > Stash0.patch

  2. 手动应用更改:

    git apply Stash0.patch

If the second step fails, you will have to edit the Stash0.patch file to fix any errors and then try git apply again.

如果第二步失败,您将不得不编辑 Stash0.patch 文件以修复任何错误,然后再次尝试 git apply。

回答by William Pursell

Either clean your working directory with git reset, commit the changes, or, if you want to stash the current changes, try:

使用 git reset 清理您的工作目录,提交更改,或者,如果您想隐藏当前更改,请尝试:

$ git stash save "description of current changes"
$ git stash pop stash@{1}

This will stash the current changes, and then pop the second stash from the stash stack.

这将存储当前更改,然后从存储堆栈中弹出第二个存储。

回答by machineghost

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

Mathias 的解决方案绝对是最接近 git stash pop --force (真的,来吧 Git 开发人员,让我们已经得到这个选项!)

However, if you want to do the same thing using only git commands, you can:

但是,如果您只想使用 git 命令来做同样的事情,您可以:

  1. git commit -a -m "Fixme"
  2. git stash pop
  3. git commit -a --amend
  4. git reset HEAD~
  1. git commit -a -m "Fixme"
  2. git stash pop
  3. git commit -a --amend
  4. 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 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.

换句话说,提交(我们永远不会推送)您当前的更改。现在你的工作区是干净的,弹出你的藏匿处。现在,提交 stash 更改作为对之前提交的修正。完成后,您现在将两组更改组合在一个提交中(“Fixme”);只需将您的结帐重置(--soft NOT --hard 所以实际上没有丢失)您的结帐到“提交之前的一个”,现在您有两组更改,完全未提交。

**EDIT**

**编辑* *

I just realized it's actually even easier; you can completely skip step 3, so ...

我才意识到这实际上更容易;你可以完全跳过第 3 步,所以......

  1. git commit -a -m "Fixme"
  2. git stash pop
  3. git reset HEAD~
  1. git commit -a -m "Fixme"
  2. git stash pop
  3. 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.)

(提交当前更改,弹出隐藏的更改,重置第一个提交以使两组更改组合为未提交状态。)

回答by Dan Rosenstark

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 reset --hard我做了多少,它让我无处可去。我的回答(无论如何都不是官方的):

  1. Figure out the stash's hash use git reflog --all
  2. Merge that hash with the branch you're interested in
  1. 弄清楚 stash 的哈希使用 git reflog --all
  2. 将该哈希与您感兴趣的分支合并

回答by mat

I also found Mathias Leppich's solutionto work great so I added an alias for it to my global .gitconfig

我还发现Mathias Leppich 的解决方案工作得很好,所以我为它添加了一个别名到我的全局 .gitconfig

[alias]
        apply-stash-to-dirty-working-tree = !git stash show -p | git apply && git stash drop

Now I can just type

现在我可以输入

git apply-stash-to-dirty-working-tree

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

(您的里程可能因这个长别名而异。但我喜欢 bash 完成时的冗长。)

回答by Chris Vandevelde

You can apply a stash to a "dirty" tree by doing a git addto stage any changes you've made, thus cleaning up the tree. Then you can git stash popand apply the stashed changes, no problem.

您可以通过执行 a 暂存您所做的git add任何更改来将存储应用于“脏”树,从而清理树。然后你可以git stash pop应用隐藏的更改,没问题。

回答by brool

You have files that have been modified but not committed. Either:

您有已修改但未提交的文件。任何一个:

git reset --hard HEAD (to bring everything back to HEAD)

or, if you want to save your changes:

或者,如果您想保存更改:

git checkout -b new_branch
git add ...
git commit
git checkout -b old_branch
git stash pop

回答by boxed

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.

我遇到了同样的问题,但是 git 更改的文件为零。原来我有一个 index.lock 文件。删除它解决了问题。