【问题标题】:Difference between git stash pop and git stash applygit stash pop 和 git stash apply 的区别
【发布时间】:2013-02-23 12:43:10
【问题描述】:

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

git stash popgit stash apply 有什么区别?

【问题讨论】:

    标签: git git-stash


    【解决方案1】:

    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 不会删除存储(并且行为与应用完全相同)
    • 看来即使你的分支中的unstaged changes和git stash pop的结果一样,你仍然会得到一个冲突错误。
    【解决方案2】:

    得到了这个有用的链接,说明了差异,正如 John Zwinck 所说的那样,git stash pop 的缺点。

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

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

    【讨论】:

    • 当弹出失败时隐藏的事实确实不是一个缺点,即使一开始可能看起来是这样。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      看到它的实际效果可能会帮助您更好地理解差异。

      假设我们正在处理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 就像堆栈的弹出一样——它实际上会在弹出元素后删除它,而apply 更像是peek

      【讨论】:

        【解决方案5】:

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

        git stash pop = git stash apply + git stash drop

        【讨论】:

          【解决方案6】:

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

          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.
          

          注意:- git apply 仅应用来自存储区域的更改,而 git pop 应用以​​及从stash 区域删除更改。

          【讨论】:

            【解决方案7】:

            快速回答:

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

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

            【讨论】:

              【解决方案8】:

              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() 只能获取顶部元素。

              【讨论】:

                猜你喜欢
                • 2017-11-25
                • 2014-03-11
                • 2015-03-11
                • 2017-11-24
                • 1970-01-01
                • 2012-12-25
                • 1970-01-01
                • 2016-04-09
                相关资源
                最近更新 更多