What Is Git Stash?
Git stash allows you to temporarily save a draft of your uncommitted changes and reset the working directory to a clean state. This can be useful when you want to switch context and work on something else without committing unfinished work.
How Git Stash Works
When you run git stash, Git saves all uncommitted changes in your working directory to a stash - essentially a stack of saved changes. This removes all uncommitted changes from your working copy so that it matches the HEAD commit again.
Stashed changes exist in a separate location until you are ready to apply them again. This allows you to switch between different branches, pull remote changes, rebase, etc., without committing unfinished work.
Git Stash Workflow
Below is an overview of common Git stash workflows.
Stashing Changes
Before stashing, stage all new or modified files so that Git can track them:
$ git add .
If you want to stash specific files, simply specify the file names when stashing:
$ git add file.txt demo.txt
Then create a new stash:
$ git stash
This saves your changes and resets your working directory to match the HEAD commit.
Naming Stashes
To give a stash a custom name when it is created, use the -m option:
$ git stash -m „<Name>"
The -m option allows you to name 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 you are ready, apply the stashed changes with 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 one:
$ git stash apply stash
Or:
$ git stash pop --index stash
Where index is the stash number from git stash list. Remember that apply keeps the stash, while pop removes it after application.
Creating a New Branch with an Applied Stash
You can create a new branch that contains the changes from a stash:
$ git stash branch Branch-Name stash
This checks out a new branch containing the stash changes, anchored to the commit where the stash was originally created.
Removing and Clearing Stashes
If you no longer need your stash, you can delete it:
$ git stash drop stash
This removes a single stash entry from the list of stash entries. To remove all stash entries, use clear:
$ git stash clear
The entries are pruned and may not be recoverable.
Additional Options
There are many additional stash options, including the ability to push stashes and save stashes. See the Git documentation for more details.
When to Use Git Stash
Git stash is ideal when you need to quickly switch between different branches or states of a project. For example:
-
Switching to another branch to pull the latest changes
-
Temporarily interrupting work to fix a hotfix or bug
-
Needing to rebase your branch and handle conflicts
-
Pausing work to merge an upstream branch into your feature branch
Stash allows you to do these things without committing half-finished work to the repository.
Summary
Git stash gives developers the ability to set aside local changes without committing them, enabling clean switching between contexts and branches. This makes stash a valuable tool for managing workflows and contexts in Git.
Need support with software development or architecture? Contact me for a no-obligation consultation.