Difference between git stash pop and git stash apply

前端 未结 7 781
忘了有多久
忘了有多久 2020-11-29 14:21

I\'ve been using git stash pop for quite some time. I recently found out about the git stash apply command. When I tried it out, it seemed to work

相关标签:
7条回答
  • 2020-11-29 14:40

    git stash pop throws away the (topmost, by default) stash after applying it, whereas git stash apply leaves it in the stash list for possible later reuse (or you can then git stash drop it).

    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.

    Another way to look at it: git stash pop is git stash apply && git stash drop.

    0 讨论(0)
  • 2020-11-29 14:45

    In git stash is a storage area where current changed files can be moved.

    stash area is useful when you want to pull some changes from git repository and detected some changes in some mutual files available in git repo.

    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.

    0 讨论(0)
  • 2020-11-29 14:49

    git stash pop applies the top stashed element and removes it from the stack. git stash apply does the same, but leaves it in the stash stack.

    0 讨论(0)
  • 2020-11-29 14:51

    Assuming there will be no errors thrown, and you want to work on the top stash item in the list of available stashes:

    git stash pop = git stash apply + git stash drop

    0 讨论(0)
  • 2020-11-29 14:52

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

    Assuming we're working on master branch and have a file hello.txt that contains "Hello" string.

    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 stash your changes:

    git stash
    

    You moved to the other branch, fixed the bug and now you're ready to continue working on your master branch, so you pop the changes:

    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 apply instead, you'll get the stashed content but you'll also keep it:

    $ 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 pop is just like stack's pop - it actually removes the element once it's popped, while apply is more like peek.

    0 讨论(0)
  • 2020-11-29 14:56

    Git Stash Pop vs apply Working

    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.

    # 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.

    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.

    0 讨论(0)
提交回复
热议问题