git stash pop
丢弃(默认情况下最顶层的)存储在应用后,而 git stash apply
将其保留在存储列表中以供以后可能重复使用(或者您可以git stash drop
它)。
除非在 git stash pop
之后发生冲突,否则会发生这种情况,在这种情况下,它不会删除存储,使其行为与 git stash apply
完全相同。
另一种看待它的方式:git stash pop
是 git stash apply && git stash drop
。
正如 John Zwinck 所说,得到了这个有用的链接来说明差异,以及 git stash pop
的缺点。
例如,假设您的隐藏更改与您自首次创建存储后所做的其他更改发生冲突。 pop 和 apply 都将有助于触发合并冲突解决模式,让您可以很好地解决此类冲突......并且两者都不会摆脱存储,即使您可能也期待 pop。由于很多人认为 stash 只是一个简单的堆栈,这通常会导致他们后来意外弹出相同的 stash,因为他们认为它已经消失了。
链接:http://codingkilledthecat.wordpress.com/2012/04/27/git-stash-pop-considered-harmful/
git stash pop
应用顶部隐藏的元素并将其从堆栈中移除。 git stash apply
做同样的事情,但将它留在存储堆栈中。
看到它在行动中可能会帮助您更好地理解差异。
假设我们正在处理 master
分支并且有一个包含“Hello”字符串的文件 hello.txt
。
让我们修改文件并在其中添加“world”字符串。现在您想要移动到另一个分支来修复您刚刚发现的一个小错误,因此您需要stash
您的更改:
git stash
您已移至另一个分支,修复了错误,现在您已准备好继续处理您的 master
分支,因此您pop
进行了更改:
git stash pop
现在,如果您尝试查看存储内容,您将获得:
$ git stash show -p
No stash found.
但是,如果您改用 git stash apply
,您将获得隐藏的内容,但您也会保留它:
$ git stash show -p
diff --git a/hello.txt b/hello.txt
index e965047..802992c 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1 @@
-Hello
+Hello world
所以 pop
就像堆栈的弹出 - 它实际上在元素弹出后删除它,而 apply
更像 peek。
假设不会抛出任何错误,并且您想要处理可用存储列表中的顶部存储项:
git stash pop
= git stash apply
+ git stash drop
快速回答:
git stash pop
->从存储列表中删除
git stash apply
->将其保存在存储列表中
git
stash 是一个存储区域,可以移动当前更改的文件。
当您想要从 git
存储库中提取一些更改并检测到 git
存储库中可用的一些相互文件中的一些更改时,stash
区域很有用。
git stash apply //apply the changes without removing stored files from stash area.
git stash pop // apply the changes as well as remove stored files from stash area.
注意:- git apply 仅应用存储区域的更改,而 git pop 应用以及删除存储区域的更改。
Git Stash Pop vs apply
工作
如果您想将最重要的隐藏更改应用到当前的非暂存更改并删除该隐藏,那么您应该选择 git stash pop
。
# apply the top stashed changes and delete it from git stash area.
git stash pop
但是,如果您想将最重要的隐藏更改应用到当前的非暂存更改而不删除它,那么您应该选择 git stash apply
。
注意:您可以将这种情况与 Stack 类 pop() 和 peek() 方法联系起来,其中 pop 通过递减 (top = top-1) 更改顶部,但 peek() 只能获取顶部元素。
git stash pop
的结果相同,您仍然会收到冲突错误。