繁体   English   中英

git stash pop 和 git stash apply 的区别

[英]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在应用后扔掉(默认情况下最顶层的)stash,而git stash apply将它留在 stash 列表中以便以后可能重用(或者你可以然后git stash drop它)。

除非在git stash pop之后发生冲突,否则会发生这种情况,在这种情况下,它不会删除 stash,让它的行为与git stash apply完全一样。

另一种看待它的方式: git stash popgit 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-thinked-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就像 stack 的 pop - 它实际上在元素被弹出后删除它,而apply更像是peek

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

当您想从git存储库中提取一些更改并检测到git repo 中可用的某些相互文件中的一些更改时, 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仅应用来自 stash 区域的更改,而git pop apply 以及从stash区域删除更改。

假设不会抛出任何错误,并且您希望处理可用存储列表中的顶部存储项目:

git stash pop = git stash apply + git stash drop

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

注意:您可以将此案例与Stackpop()peek()方法联系起来,其中 pop 通过递减(top = top-1)更改顶部,但peek()只能获取顶部元素。

快速回答:

git stash pop -> 从存储列表中删除

git stash apply -> 将其保存在 stash 列表中

当我遇到“存储问题”时,我会使用 git stash branch new-branch。 它会创建一个包含所有更改的新分支,然后您可以放弃其他分支或其他分支上的更改。 当我无法检索隐藏的更改时,这对我有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM