How to use the git stash pop command

What is the git stash pop command?

The git stash pop command lets a developer move their most recently shelved changes into their working directory, and also delete that popped stash upon command completion.

The syntax for the git stash pop command is simple and self-evident:

> git stash pop

However, you must first add items to Git’s stash to pop your shelved changes out of it. That’s exactly what we will show you how to do next.

git stash pop cheat sheet

The git stash allows developers to shelve changes and pop them back later.

Why would you use the git stash?

Sometimes a developer updates a file or makes changes that are not ready for a commit to be shared with the rest of the team. At the same time, the developer must perform a commit or a reset to perform other work, such as the following:

  • Switch between branches.
  • Fetch or pull new code from GitHub or GitLab.
  • Cherry-pick from another branch.
  • Immediately fix a bug.

With the git stash command, a developer can move onto other work that would normally require an empty Git index and a clean working directory, while not lose recent changes or updates made since the last Git commit.

How do you shelve changes in Git?

To move all changes made to tracked and staged files since the last Git commit, simply issue the git stash push command in the terminal window as follows:

git stash push
git stash command

The git stash pop and git stash apply commands are considered part of the Git porcelain.

Name a Git stash

It’s possible to accumulate a large number of entries in your Git stash, so it makes sense to name each entry in the stash.

To accomplish this, add a git stash message with the -m flag:

git stash push --message "Git stash pop tutorial"

How do you stash untracked files?

By default, the git stash push command only operates on tracked files and files staged in the Git index. To stash untracked files, add the -u or the –include-untracked switch:

git stash push --include-untracked

After the git stash push command completes, your workspace resets back to the way it was when the last Git commit was created.

stash and pop untracked files

You must use the -u switch to add untracked files to Git stash.

How do you use the git stash pop command?

After a developer moves their changes into the stash, they are free to push, pull, fetch, revert and cherry-pick all they want. When they want the shelved changes back, they just issue the git stash pop command:

git stash pop

This moves all shelved changes back into the workspace, and at the same time deletes that particular entry from the stash list.

git stash pop example

The best way to learn commands such as git stash pop is to actually use them.

The nine commands below provide a start-to-finish git stash pop example that accomplish the following tasks:

  • Create a Git repository.
  • Add files and commits.
  • Update a tracked file.
  • Stash the update.
  • Perform further changes and commits.
  • Pull the shelved changes back with a git stash pop.

Give these commands a try to really learn how the git stash pop command works:

git init
echo "Git stash pop command." >> example.txt
git add .
git commit -m "First git stash pop example commit"
echo "Updated git stash pop tutorial." >> example.txt
git stash
rm example.txt
git commit -am "updated the stash pop example file"
git stash pop

How do you handle git stash conflicts?

It’s possible to encounter a conflict if a file you move from the stash into your working directory has been updated and made part of a commit since the stash to pop was originally created.

To resolve a git stash merge conflict, do the following:

  • Find and open the file in conflict.
  • Update the file to rectify the conflicting changes.
  • Add the updated file to the index.
  • Perform a commit.

After the commit occurs, the git stash conflict will resolve and you can resume performing normal Git operations.

git stash pop conflict

To resolve a git stash pop conflict, just update the file in conflict and then perform a commit.

What’s the difference between git stash pop and apply?

There are actually two ways to move changes from the stash into your working directory:

  • git stash pop
  • git stash apply

The difference between git stash pop versus git stash apply is that pop deletes the stash after files are moved into your working directory, while apply does not.

Furthermore, since the git stash pop command deletes the topmost stash when done, the ordering of all the stashes in your stash history is updated accordingly.

Those are the only differences between git stash pop and git stash apply.

git stash apply vs pop

Git stash pop removes items from the stash history, while git stash apply does not.

How git stash and pop work together

When you need to temporarily shelve changes but know you’ll return to them sometime in the near future, Git’s stash feature is a godsend.

And when you want to bring those shelved changes back into your development workspace, that’s what the git stash pop command is for.

App Architecture
Software Quality
Cloud Computing