Sandeep Raju Prabhakar
Sandeep Raju Prabhakar

Reputation: 20112

Difference between git stash pop and git stash apply

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?

Upvotes: 1658

Views: 869424

Answers (10)

Maroun
Maroun

Reputation: 95948

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.

Upvotes: 70

gauravcode
gauravcode

Reputation: 1

# --> git stash apply
# -------------------
# git stash apply takes the changes from a stash entry (usually the most recent one)
# and applies them to your working directory.

# Keeps the Stash Entry:
# ----------------------
# It doesn’t delete the stash entry. The stash remains in the stash list, 
# so you can apply it again if you want to.

# When to Use:
# ------------
# Use `apply` if you want to bring the changes back but keep the stash around 
# just in case you might need it again.

# --> git stash pop
# -----------------
# What it does:
# -------------
# git stash pop is similar—it applies the stash changes to your working directory.

# Removes the Stash Entry:
# ------------------------
# After applying the changes, `pop` deletes the stash entry. 
# It’s a bit like applying and cleaning up in one step.

# When to Use:
# ------------
# Use `pop` when you’re done with the stash and don’t need to keep it around anymore.


# Example:

# Before `git stash apply` or `git stash pop`:
# --------------------------------------------
# | Stash List       | Working Directory Changes  |
# |------------------|----------------------------|
# | stash@{0}        | None                       |
# | stash@{1}        |                            |
# -----------------------------------------------

# After `git stash apply`:
# ------------------------
# | Stash List       | Working Directory Changes       |
# |------------------|----------------------------------|
# | stash@{0}        | Applied changes from stash@{0}  |
# | stash@{1}        |                                  |
# -----------------------------------------------------
# - `git stash apply` keeps `stash@{0}` in the list.

# After `git stash pop`:
# ----------------------
# | Stash List       | Working Directory Changes       |
# |------------------|----------------------------------|
# | stash@{1}        | Applied changes from stash@{0}  |
# -----------------------------------------------------
# - `git stash pop` removes `stash@{0}` from the list after applying.

Upvotes: 0

EMEM
EMEM

Reputation: 3138

Quick Answer:

git stash pop -> apply and remove from the stash list

git stash apply -> apply and keep it in the stash list

Upvotes: 68

Prem
Prem

Reputation: 93

git stash is like a cache used to store our work temporarily.

git stash -> works like Cut (Ctrl + X) and stores it in stash memory.

git stash apply -> works like paste (Ctrl + V) and doesn't clear stash memory. [ can be used multiple times ]

git stash pop -> works like paste (Ctrl + V) and clears the cache/stash memory. [ one time use only ]

Upvotes: 6

Jean Patricio
Jean Patricio

Reputation: 499

You can think in that same way, this is how I learned:

git stash pop -> ctrl + x, ctrl + v. (cut and paste)

git stash apply -> ctrl + c, ctrl + v. (copy and paste)

Upvotes: 24

Greeso
Greeso

Reputation: 8219

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

Upvotes: 63

briankip
briankip

Reputation: 2640

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/

Upvotes: 107

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

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.

Upvotes: 18

John Zwinck
John Zwinck

Reputation: 249123

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.

Upvotes: 2591

jchapa
jchapa

Reputation: 3996

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.

Upvotes: 104

Related Questions