适用于git stash pop和git stash的区别

Difference between git stash pop and git stash apply

我已经使用git stash pop很长一段时间了。 我最近发现了git stash apply命令。 当我尝试它时,它似乎与git stash pop一样。

git stash popgit stash apply之间有什么区别?


git stash pop在应用它之后抛弃(默认情况下最顶层)存储,而git stash apply将它留在存储列表中以便以后重用(或者你可以git stash drop它)。

除非在git stash pop之后发生冲突,否则会发生这种情况,在这种情况下,它不会删除存储,使其行为与git stash apply完全相同。

另一种看待它的方法:git stash popgit stash apply && git stash drop


得到了这个有用的链接,说明了差异,正如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. 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.

链接http://codingkilledthecat.wordpress.com/2012/04/27/git-stash-pop-considered-harmful/


git stash pop应用顶部隐藏元素并将其从堆栈中删除。 git stash apply执行相同操作,但将其保留在存储堆栈中。


看到它在行动可能会帮助您更好地理解差异。

假设我们正在处理master分支并且文件hello.txt包含"Hello"字符串。

让我们修改文件并为其添加"world"字符串。现在您想要移动到另一个分支来修复您刚刚找到的小错误,因此您需要stash您的更改:

1
git stash

您移动到另一个分支,修复了错误,现在您已准备好继续处理master分支,因此pop更改:

1
git stash pop

现在,如果您尝试查看您将获得的藏匿内容:

1
2
$ git stash show -p
No stash found.

但是,如果您使用git stash apply,您将获得隐藏的内容,但您也会保留它:

1
2
3
4
5
6
7
8
$ 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更像是窥视。


Git Stash Pop vs apply工作

如果您想将最高级别的更改应用于当前的非暂存更改并删除该存储,那么您应该转到git stash pop

1
2
# apply the top stashed changes and delete it from git stash area.
git stash pop

但是如果你想在不删除它的情况下将你的顶级存储更改应用到当前的非分段更改,那么你应该去git stash apply

Note : You can relate this case with Stack class pop() and peek() methods, where pop change the top by decrements (top = top-1) but peek() only able to get the top element.


git stash中是一个存储区域,可以移动当前更改的文件。

当您想要从git存储库中提取一些更改并检测到git repo中可用的某些相互文件中的某些更改时,stash区域非常有用。

1
2
3
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.

Note :- git apply only apply the changes from stash area while git pop apply as well as remove change from stash area.