BudiBadu Logo
Samplebadu

Git by Example: Stash Pop

2.43.0

Learn how to apply stashed changes and immediately remove them from the stash list. This is the most common way to retrieve your work and clean up.

Code

# 1. List available stashes
git stash list

# Output:
# stash@{0}: WIP on main: 1a2b3c Fix login bug

# 2. Pop the stash (apply and drop)
git stash pop

# Output:
# On branch main
# Changes not staged for commit:
#   modified:   login.js
# Dropped refs/stash@{0} (2b3c4d...)

# 3. Verify the stash is gone
git stash list

# Output:
# (empty, or shows older stashes)

Explanation

The git stash pop command is a convenient shortcut that combines two actions: applying the stash and then immediately dropping it. It attempts to take the changes from the most recent stash (or a specific one), apply them to your current working directory, and if successful, delete that stash entry from the list.

This command is perfect for the standard workflow: "I need to pause this work, switch contexts, and then come back and finish it." When you "pop" the stash, you are effectively saying, "I'm ready to resume this work, so give me my changes back and remove them from the temporary shelf." It keeps your stash list clean automatically.

However, there is a catch: if git stash pop encounters a merge conflict while applying the changes, it will not drop the stash. This is a safety feature. Git leaves the stash in the list so that if you mess up resolving the conflict, you can still try again. You'll need to resolve the conflict and then manually run git stash drop.

  • Applies changes AND removes stash from list
  • Best for resuming work ("pick up where left off")
  • Keeps stash list clean
  • Does NOT drop stash if conflicts occur

Code Breakdown

8
Executes the pop operation. It modifies your files and, if successful, prints "Dropped refs/stash@{0}".
14
The output confirms that the stash was removed. The list is now empty (or contains only older items).