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

San*_*kar 894 git git-stash

我已经使用git stash pop了很长一段时间.我最近发现了这个git stash apply命令.当我尝试它时,它似乎工作相同git stash pop.

git stash pop和之间有什么区别git stash apply

Joh*_*nck 1419

git stash pop 在应用它之后扔掉(最上面的,默认情况下)存储,然后将它git stash apply 留在存储列表中以便以后重用(或者你可以然后git stash drop它).

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

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

  • 如下面的@ briankip的回答说明,如果在弹出存储时存在冲突,pop将不会删除存储(并且行为与apply非常相似) (103认同)

bri*_*kip 75

得到了这个有用的链接,说明了差异,正如John Zwinck所述,以及Git stash pop的缺点.

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

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

  • 当一个流行音乐失败时,持续存在的事实并不是一个缺点,即使它最初可能看起来像这样. (6认同)

jch*_*apa 60

git stash pop应用顶部隐藏元素并将其从堆栈中删除.git stash apply做同样的事情,但把它留在藏匿堆栈中.


Ehs*_*adi 56

快速回答:

git stash pop-> 从隐藏列表中删除

git stash apply-> 将其保留在隐藏列表中


Mar*_*oun 40

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

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

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

git stash
Run Code Online (Sandbox Code Playgroud)

您移动到另一个分支,修复了错误,现在您已准备好继续在您的master分支上工作,因此您需要pop进行更改:

git stash pop
Run Code Online (Sandbox Code Playgroud)

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

$ git stash show -p
No stash found.
Run Code Online (Sandbox Code Playgroud)

但是,如果您使用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
Run Code Online (Sandbox Code Playgroud)

所以pop就像stack的pop一样 - 它实际上会在弹出元素时删除它,而apply更像是peek.


Jea*_*cio 16

你也可以用同样的方式思考,这就是我学到的:

git stash pop -> ctrl + x, ctrl + v。(剪切和粘贴)

git stash apply -> ctrl + c, ctrl + v。(复制和粘贴)


Vik*_*yap 11

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

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.
Run Code Online (Sandbox Code Playgroud)

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


Gre*_*eso 9

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

git stash pop= git stash apply+git stash drop


Vik*_*yap 6

Git 存储Pop vs apply工作

如果您想将最重要的隐藏更改应用于当前的非暂存更改并删除该隐藏更改,那么您应该选择git stash pop.

# apply the top stashed changes and delete it from git stash area.
git stash pop  
Run Code Online (Sandbox Code Playgroud)

但是,如果您想将最重要的隐藏更改应用到当前的非暂存更改而不删除它,那么您应该选择git stash apply.

注意:您可以将这种情况与Stackpop()peek()方法联系起来,其中 pop 按减量更改顶部(top = top-1),但peek()只能获取顶部元素。