git stash pop 和 git stash apply 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15286075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:41:01  来源:igfitidea点击:

Difference between git stash pop and git stash apply

gitgit-stash

提问by Sandeep Raju Prabhakar

I've been using git stash popfor quite some time. I recently found out about the git stash applycommand. When I tried it out, it seemed to work the same as git stash pop.

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

What is the difference between git stash popand git stash apply?

git stash pop和 和有git stash apply什么区别?

回答by John Zwinck

git stash popthrows awaythe (topmost, by default) stash after applying it, whereas git stash applyleaves it in the stash listfor possible later reuse (or you can then git stash dropit).

git stash pop应用后丢弃(默认情况下最顶层的)存储,而git stash apply将其留在存储列表中以备以后可能重用(或者您可以git stash drop)。

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 popis git stash apply && git stash drop.

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

回答by briankip

Got this helpful link that states the difference, as John Zwinck has stated and a drawback of git stash pop.

得到了这个有用的链接,它说明了差异,正如约翰·兹温克所说,以及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 too. 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.

例如,假设您的隐藏更改与您自第一次创建存储后所做的其他更改发生冲突。pop 和 apply 都将有助于触发合并冲突解决模式,允许您很好地解决此类冲突……而且两者都不会摆脱存储,即使您可能也期待 pop。由于很多人希望 stash 只是一个简单的堆栈,这通常会导致他们稍后意外弹出相同的 stash,因为他们认为它已经消失了。

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

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

回答by jchapa

git stash popapplies the top stashed element and removes it from the stack. git stash applydoes the same, but leaves it in the stash stack.

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

回答by Maroun

Seeing it in action might help you better understanding the difference.

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

Assuming we're working on masterbranch and have a file hello.txtthat contains "Hello" string.

假设我们在master分支上工作并且有一个hello.txt包含“Hello”字符串的文件。

Let's modify the file and add " world" string to it. Now you want to move to a different branch to fix a minor bug you've just found, so you need to stashyour changes:

让我们修改文件并向其添加“world”字符串。现在你想移动到一个不同的分支来修复你刚刚发现的一个小错误,所以你需要stash你的改变:

git stash

You moved to the other branch, fixed the bug and now you're ready to continue working on your masterbranch, so you popthe 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 applyinstead, 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 popis just like stack's pop - it actually removes the element once it's popped, while applyis more like peek.

所以pop就像堆栈的弹出 - 它实际上在弹出后删除元素,而apply更像是peek

回答by Vikrant Kashyap

In gitstashis a storage area where current changed files can be moved.

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

stasharea is useful when you want to pull some changes from gitrepository and detected some changes in some mutual files available in gitrepo.

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

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 applyonly apply the changes from stash area while git popapply as well as remove change from stasharea.

注意:-git apply仅应用存储区域的更改,同时git pop应用和删除stash区域的更改。

回答by Vikrant Kashyap

Git Stash Pop vs applyWorking

Git 存储Pop vs apply工作

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 Stackclass pop()and peek()methods, where pop change the top by decrements (top = top-1) but peek()only able to get the top element.

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