我在我的私房钱里存了一小块。我已经使用git stash apply将它应用到我的工作副本。现在,我想通过反向应用补丁来撤销这些更改(有点像git revert会做的事情,但针对的是stash)。

有人知道怎么做吗?

澄清:在我的工作副本中有其他更改。我的具体情况很难描述,但您可以想象在存储库中有一些调试或实验代码。现在,它混合在我的工作副本与其他一些变化,我想看看效果与不从隐藏的变化。

目前看来stash还不支持这个功能,但是git的stash apply—reverse将是一个不错的功能。


当前回答

对我来说,签出时我刚刚输入了错误的存储库名称。所以根本就没有遥控器可以拉。

因此,除了检查大小写之外,还要检查分支名称的拼写,或者更好的方法是复制并粘贴它,以排除它。

其他回答

如何反向应用收藏?

除了别人提到的,最简单的方法是先做

git reset HEAD

然后签出所有本地更改

git checkout . 
git stash show -p | git apply --reverse

警告,这不会在所有情况下:“git apply -R”(man)不能正确处理两次触及同一路径的补丁,这已在git 2.30(2021年第一季度)中得到纠正。

这在将路径从常规文件更改为符号链接(反之亦然)的补丁中是最相关的。

参见Jonathan Tan (jhowtan)提交b0f266d(2020年10月20日)。 (由Junio C Hamano - gitster -在commit c23cd78中合并,2020年11月2日)

apply:当-R时,也是反向的section列表 协助:Junio C Hamano 署名:乔纳森·谭

A patch changing a symlink into a file is written with 2 sections (in the code, represented as "struct patch"): firstly, the deletion of the symlink, and secondly, the creation of the file. When applying that patch with -R, the sections are reversed, so we get: (1) creation of a symlink, then (2) deletion of a file. This causes an issue when the "deletion of a file" section is checked, because Git observes that the so-called file is not a file but a symlink, resulting in a "wrong type" error message. What we want is: (1) deletion of a file, then (2) creation of a symlink. In the code, this is reflected in the behavior of previous_patch() when invoked from check_preimage() when the deletion is checked. Creation then deletion means that when the deletion is checked, previous_patch() returns the creation section, triggering a mode conflict resulting in the "wrong type" error message. But deletion then creation means that when the deletion is checked, previous_patch() returns NULL, so the deletion mode is checked against lstat, which is what we want. There are also other ways a patch can contain 2 sections referencing the same file, for example, in 7a07841c0b ("git-apply: handle a patch that touches the same path more than once better", 2008-06-27, Git v1.6.0-rc0 -- merge). "git apply -R"(man) fails in the same way, and this commit makes this case succeed. Therefore, when building the list of sections, build them in reverse order (by adding to the front of the list instead of the back) when -R is passed.

我自己也有类似的问题,我认为你所需要做的就是git重置——很难,你不会失去你的变化或任何未跟踪的变化。

如果你阅读git stash——help中的文档,它声明apply是“像pop一样,但不从stash列表中删除状态”,所以状态仍然驻留在那里,你可以取回它。

或者,如果没有冲突,您可以在测试更改后再次git stash。

如果你确实有冲突,不要担心,git重置-hard不会失去他们,因为 “应用状态可能会因冲突而失败;在这种情况下,它不会从收藏列表中删除。你需要手动解决冲突,然后手动调用git stash drop。”

这是对上述答案的补充,但增加了基于消息的git存储的搜索,因为存储数可以在保存新存储时更改。 我写了几个bash函数:

apply(){
  if [ "$1" ]; then
    git stash apply `git stash list | grep -oPm1 "(.*)(?=:.*:.*$1.*)"`
  fi
}
remove(){
  if [ "$1" ]; then
    git stash show -p `git stash list | grep -oPm1 "(.*)(?=:.*:.*$1.*)"` | git apply -R
    git status
  fi
}

创建存储名称(消息)$ git存储保存为“我的存储” 命名为$ apply "my stash" 删除“my stash”

对我来说,签出时我刚刚输入了错误的存储库名称。所以根本就没有遥控器可以拉。

因此,除了检查大小写之外,还要检查分支名称的拼写,或者更好的方法是复制并粘贴它,以排除它。