本文翻译自:Difference between git stash pop and git stash apply
I've been using git stash pop
for quite some time. 我一直在使用git stash pop
一段时间了。 I recently found out about the git stash apply
command. 我最近发现了git stash apply
命令。 When I tried it out, it seemed to work the same as git stash pop
. 当我尝试它时,它似乎与git stash pop
。
What is the difference between git stash pop
and git stash apply
? git stash pop
和git stash apply
什么区别?
参考:https://stackoom.com/question/128bb/适用于git-stash-pop和git-stash的区别
git stash pop
throws away the (topmost, by default) stash after applying it, whereas git stash apply
leaves it in the stash list for possible later reuse (or you can then git stash drop
it). git stash pop
在应用它之后抛弃 (最上面,默认情况下)stash,而git stash apply
将它留在隐藏列表中以便以后重用(或者你可以git stash drop
it)。
This happens unless there are conflicts after git stash pop
, in which case it will not remove the stash, leaving it to behave exactly like git stash apply
. 除非在git stash pop
之后发生冲突,否则会发生这种情况,在这种情况下它不会删除存储,使其行为与git stash apply
完全相同。
Another way to look at it: git stash pop
is git stash apply && git stash drop
. 另一种看待它的方法: git stash pop
是git stash apply && git stash drop
。
git stash pop
applies the top stashed element and removes it from the stack. git stash pop
应用顶部stashed元素并将其从堆栈中删除。 git stash apply
does the same, but leaves it in the stash stack. git stash apply
也是如此,但是将它留在了存储堆栈中。
Got this helpful link that states the difference, as John Zwinck has stated and a drawback of Git stash pop. 得到了这个有用的链接,说明了差异,正如John Zwinck所述,以及Git stash pop的缺点。
For instance, say your stashed changes conflict with other changes that you've made since you first created the stash. 例如,假设您的隐藏更改与您自首次创建存储后所做的其他更改发生冲突。 Both pop and apply will helpfully trigger merge conflict resolution mode, allowing you to nicely resolve such conflicts… and neither will get rid of the stash, even though perhaps you're expecting pop to. pop和apply都会有助于触发合并冲突解决模式,让你很好地解决这些冲突......并且也不会摆脱存储,即使你可能期望流行。 Since a lot of people expect stashes to just be a simple stack, this often leads to them popping the same stash accidentally later because they thought it was gone. 由于很多人都认为藏匿只是一个简单的堆栈,这通常会导致他们后来意外地弹出相同的藏匿处,因为他们认为它已经消失了。
Link http://codingkilledthecat.wordpress.com/2012/04/27/git-stash-pop-considered-harmful/ 链接http://codingkilledthecat.wordpress.com/2012/04/27/git-stash-pop-considered-harmful/
Seeing it in action might help you better understanding the difference. 看到它在行动可能会帮助您更好地理解差异。
Assuming we're working on master
branch and have a file hello.txt
that contains "Hello" string. 假设我们正在使用master
分支并且有一个包含“Hello”字符串的文件hello.txt
。
Let's modify the file and add " world" string to it. 让我们修改文件并为其添加“world”字符串。 Now you want to move to a different branch to fix a minor bug you've just found, so you need to stash
your changes: 现在您想要移动到另一个分支来修复您刚刚找到的小错误,因此您需要stash
您的更改:
git stash
You moved to the other branch, fixed the bug and now you're ready to continue working on your master
branch, so you pop
the changes: 您移动到另一个分支,修复了错误,现在您已准备好继续处理master
分支,因此您可以pop
更改:
git stash pop
Now if you try to review the stash content you'll get: 现在,如果您尝试查看您将获得的藏匿内容:
$ git stash show -p
No stash found.
However, if you use git stash apply
instead, you'll get the stashed content but you'll also keep it: 但是,如果您使用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
So pop
is just like stack's pop - it actually removes the element once it's popped, while apply
is more like peek . 因此pop
就像stack的pop一样 - 它实际上会在弹出元素时删除它,而apply
更像是peek 。
Git Stash Pop vs apply
Working Git Stash Pop vs apply
Working
If you want to apply your top stashed changes to current non-staged change and delete that stash as well, then you should go for git stash pop
. 如果你想对你当前的非分段更改应用你的顶级存储更改并删除该存储,那么你应该去git stash pop
。
# apply the top stashed changes and delete it from git stash area.
git stash pop
But if you are want to apply your top stashed changes to current non-staged change without deleting it, then you should go for git stash apply
. 但是如果你想在不删除它的情况下将你的顶级存储更改应用到当前的非分段更改,那么你应该去git stash apply
。
Note : You can relate this case with
Stack
classpop()
andpeek()
methods, where pop change the top by decrements (top = top-1) butpeek()
only able to get the top element. 注意:您可以将此案例与Stack
类pop()
和peek()
方法联系起来,其中pop通过减量(top = top-1)更改顶部但peek()
仅能够获取顶部元素。