Difference between Git Stash Pop and Git Stash Apply

Better Stack Team
Updated on July 25, 2024

The commands git stash pop and git stash apply are both used to retrieve stashed changes (stored in the stash) back into your working directory. However, they differ in how they handle the retrieved changes and the state of the stash afterward.

git stash pop

The git stash pop command retrieves the most recently stashed changes and removes them from the stash:

 
git stash pop
  • Behavior:
    • Retrieves the most recent stash (stash@{0}) and applies it to your working directory.
    • Removes the retrieved stash from the stash list (stash@{0} is deleted).
  • Use Case:
    • Useful when you want to retrieve and immediately remove the most recent stash.
    • Automatically cleans up the stash after applying the changes.
  • Example:

     
    git stash pop
    

git stash apply

The git stash apply command retrieves stashed changes and applies them to your working directory without removing them from the stash:

 
git stash apply
  • Behavior:
    • Retrieves the most recent stash (stash@{0}) and applies it to your working directory.
    • Does not remove the retrieved stash from the stash list (stash@{0} remains in the stash).
  • Use Case:
    • Useful when you want to apply stashed changes but keep the stash for future reference or further manipulation.
    • Allows multiple stashes to be applied sequentially without losing them.
  • Example:

     
    git stash apply
    

Comparison and Usage

  • Safety: If you're unsure about the outcome or if multiple people might access the same stash, git stash apply is safer since it doesn't delete the stash.
  • Cleanup: git stash pop is convenient for a quick retrieval and cleanup of stashed changes.
  • Multiple Stashes: Use git stash apply when you need to apply changes from multiple stashes or when you want to keep the stash for later use.

Additional Options

Both git stash pop and git stash apply can accept a stash reference (stash@{n}) to apply or pop a specific stash instead of the most recent one. For example:

 
git stash pop stash@{1}
git stash apply stash@{2}

These commands retrieve and apply the second and third stashes respectively, without affecting other stashes in the list.

In summary, the choice between git stash pop and git stash apply depends on whether you want to remove the stash after applying its changes (git stash pop) or keep it for potential future use (git stash apply). Both commands are essential tools for managing temporary changes in Git workflows.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github