What is git stash?

Git stash allows you to temporarily save a draft of your changes that have not yet been committed, and revert the working directory to a clean state. This can be useful when you want to switch context and work on something else, but don't want to commit unfinished work.

How Git Stash Works

When you run git stash, Git takes all of the uncommitted changes in your working directory and saves them in a stash - essentially a stack of stashed changes. This removes all uncommitted changes from your working copy so it matches the HEAD commit again.

Stashed changes exist in a separate place until you are ready to re-apply them. This allows you to context switch between different branches, pull remote changes, rebase, etc. without having to commit unfinished work.

Git stash workflow

Here is an overview of common Git stash workflows.

Stashing Changes

Before stashing, stage any new or modified files so Git tracks them:

$ git add .

If you want stash specific files, just specify the filenames when staging:

$ git add file.txt demo.txt

Then create a new stash:

$ git stash

This saves your changes and reverts your working directory so it matches the HEAD commit.

Naming Stashes

To give a custom name to a stash when creating it use the -m option:

$ git stash -m "<name>"

The -m option allows naming the stash for easier identification later.

Viewing Stashes

To see a list of all your stashes:

$ git stash list

You can view a summary of a stash with git stash show

$ git stash show 

Where <stash> is the stash reference shown in 'git stash list'.

Applying Stashes

Later when ready, re-apply the stashed changes using git stash pop

$ git stash pop

This applies the changes and removes the stash from the list. To keep the stash, use git stash apply instead.

$ git stash apply

Applying specific stashes

To apply a stash other than the most recent:

$ git stash apply <stash>

Or:

$ git stash pop --index <stash>

Where <index> is the stash number from 'git stash list'. Keep in mind-using apply keeps the stash, while pop removes it after applying.

Creating a new branch with a stash applied

You can create a new branch containing a stash's changes using:

$ git stash branch <branch-name> <stash>

This checks out a new branch containing the stash changes anchored at the commit where the stash was originally created.

Removing and clearing stashes

When you no longer need your stash you can drop it using:

$ git stash drop <stash>

This removes a single stash entry from the list of stash entries. To remove all the stash entries, use clear :

$ git stash clear

The entries will be subjected to pruning, and maybe impossible to recover.

Additional Options

There are many additional stash options, including the ability to push stashes and store stashes. Refer to Git documentation for more details.

When to Use Git Stash

Git stash is ideal when you need to quickly context switch between different branches or states of a project. For example:

  • Switching to another branch to pull latest changes
  • Temporarily shelving work to address a hotfix or bug
  • Rebasing your branch and needing to deal with conflicts
  • Pausing work to merge an upstream branch into your feature branch

Stash allows you to do these things without committing half-done work to the repository.

Conclusion

Git stash gives developers a way to set aside local changes without committing, enabling you to switch contexts and branches cleanly. This makes stash a valuable tool for managing workflow and contexts in Git.


Weekly Catch Ups is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.

Subscribe to sysxplore newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox. It's free!
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!