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 the same as git stash pop
.
What is the difference between git stash pop
and git stash apply
?
git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.
Retrieving stashed changes You can reapply stashed changes with the commands git stash apply and git stash pop . Both commands reapply the changes stashed in the latest stash (that is, stash@{0} ). A stash reapplies the changes while pop removes the changes from the stash and reapplies them to the working copy.
The key difference between git stash pop and apply involves the stash history. When a developer uses the git stash apply command, the most recently saved stash overwrites files in the current working tree but leaves the stash history alone. In contrast, the pop command restores files but then deletes the applied stash.
Stashing Your WorkYou can save a stash on one branch, switch to another branch later, and try to reapply the changes. You can also have modified and uncommitted files in your working directory when you apply a stash — Git gives you merge conflicts if anything no longer applies cleanly.
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
.
Got this helpful link that states the difference, as John Zwinck has stated and a drawback of 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.
Link: http://codingkilledthecat.wordpress.com/2012/04/27/git-stash-pop-considered-harmful/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With