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

发布于 2024-08-03 07:05:24 字数 158 浏览 10 评论 0原文

我正在尝试应用之前使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

望笑 2024-08-10 07:05:24

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

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

基本上,它

  1. 会创建一个补丁
  2. 则将其发送到应用命令
  3. 管道,如果存在任何需要解决的冲突, 通过三向合并,
  4. 如果应用(或合并)成功,它会删除刚刚应用的存储项...

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

与此同时,您可能想将这一行添加为 git 别名:

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

感谢 @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:

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

I wonder why there is no -f (force) option for git 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:

$ 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 -3 parameter which allows to resolve conflicts directly via 3-way merge.

攀登最高峰 2024-08-10 07:05:24

我这样做:

git add -A
git stash apply

然后(可选):

git reset

I do it in this way:

git add -A
git stash apply

and then (optionaly):

git reset
幻梦 2024-08-10 07:05:24

您可以通过将所需的存储导出为补丁文件并手动应用它来执行此操作,而无需存储当前的更改。

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

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

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


  2. 手动应用更改:

    git apply Stash0.patch
    

如果第二步失败,您必须编辑 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:

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

    git stash show -p stash@{0} > Stash0.patch
    
  2. Manually apply the changes:

    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.

自在安然 2024-08-10 07:05:24

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

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

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

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

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

悲喜皆因你 2024-08-10 07:05:24

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

但是,如果您只想使用Git命令,你可以:

  1. git commit -a -m "Fixme"
  2. git stash pop
  3. git commit -a --amend
  4. git重置 HEAD~

换句话说,对当前的更改进行提交(我们永远不会推送)。现在你的工作空间已经干净了,可以打开你的储藏室了。现在,提交存储更改作为对之前提交的修改。完成此操作后,您现在可以将两组更改合并到一次提交中(“Fixme”);只需 git reset--soft 不是 --hard 所以实际上没有丢失任何内容)您签出到“提交之前的一个”,现在您拥有两组完全未提交的更改。

编辑

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

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

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

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

聚集的泪 2024-08-10 07:05:24

如果您发现自己像我今天一样处于这种情况,那么这些答案实际上都不起作用。不管我做了多少次 git reset --hard ,都没有任何结果。我的答案(无论如何都不是官方的):

  1. 找出存储的哈希值,使用 git reflog --all
  2. 将该哈希值与您感兴趣的分支合并

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

  1. Figure out the stash's hash use git reflog --all
  2. Merge that hash with the branch you're interested in
旧情勿念 2024-08-10 07:05:24

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

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

现在我只需输入

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

对我来说非常有用的内容即可。

(你对这个长别名的理解可能会有所不同。但我喜欢 bash 完成时的冗长。)

I also found Mathias Leppich's solution to work great so I added an alias for it to my global .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.)

清晰传感 2024-08-10 07:05:24

您可以通过执行 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 can git stash pop and apply the stashed changes, no problem.

吹泡泡o 2024-08-10 07:05:24

您有已修改但未提交的文件。或者:

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

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

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

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
水染的天色ゝ 2024-08-10 07:05:24

我遇到了同样的问题,但 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.

天涯沦落人 2024-08-10 07:05:24

我无法让其中大部分发挥作用;由于某种原因,它总是认为我对文件进行了本地更改。我无法应用存储,补丁也不会应用,checkoutreset --hard 失败。最终起作用的是将存储保存为使用 git stashbranchtempbranchname 的分支,然后进行正常的分支合并:git checkout master 和 git merge tempbranchname代码>.
来自 http://git-scm.com/book/en/Git-Tools-隐藏

如果您想要一种更简单的方法来再次测试隐藏的更改,您可以
运行 git stashbranch,它会为您创建一个新分支,签出
当您隐藏工作时所做的提交会重新应用您的工作
在那里,如果应用成功,则将其丢弃

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 and reset --hard fail. What finally worked was saving the stash as a branch with git stash branch tempbranchname, and then doing a normal branch merge: git checkout master and git merge tempbranchname.
From http://git-scm.com/book/en/Git-Tools-Stashing :

If you want an easier way to test the stashed changes again, you can
run git stash branch, which creates a new branch for you, checks out
the commit you were on when you stashed your work, reapplies your work
there, and then drops the stash if it applies successfully

╰つ倒转 2024-08-10 07:05:24

这就是我对这个问题的看法,对我来说没有问题。

git stash show | patch -p1

当然,它部分地求助于 git 命令系列之外的命令。

That's my take on the problem that worked for me without problems.

git stash show | patch -p1

Of course it partially resorts to a command outside the git commands family.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文