Introduction
A Git stash represents a great way to locally set aside unfinished work without committing changes. Stashing allows developers to focus on a burning issue, such as a bug fix or an urgent patch.
Naming a Git stash helps users find the right stash later, especially if there are multiple stashes.
In this tutorial, you will learn to name a Git stash and retrieve it.
Prerequisites
- Git installed (follow our tutorial and install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
How to Add a Name to Git Stash
When creating a stash using the git stash
command, Git automatically generates a stash name and assigns a generic message. The issue with generic names is that they are all similar, and when there are multiple stashes in a repository, it is difficult to find a particular one.
For example, all the stashes in the following repository have an automatically generated name:
Listing the stashes outputs a list of similar names, differentiating only in the stash index number. In the example above all the stashes were created on the same commit, which makes it even more difficult to differentiate one stash from another.
To easily find a Git stash, add a custom name to the stash or a message by specifying the -m
option. The syntax is:
git stash push -m [message]
Encase the [message]
in double quotes (""
). For example:
In the example above, we added a message that describes the changes we stashed.
Note: The git save
command was used for stashing changes, but it is now deprecated, and the git push
command is preferred. However, the save method is still used in some legacy systems and older shell scripts.
Check if the message was added by listing existing Git stashes. Run:
git stash list
How to Retrieve a Git Stash by Name
There are two ways to retrieve a Git stash:
- Use the Git stash name, i.e., via regular expressions.
- Specify the stash index.
When retrieving a stash by its name, Git only allows you to apply it. Retrieving a stash using the stash index allows you to either pop it or apply it. When popping the stash, it is deleted from the local ref, while applying a stash retrieves the changes but doesn't delete the stash from the ref.
The following sections show how to retrieve a Git stash.
1. Using Regex
Retrieve a stash using regular expressions by specifying the stash name. The syntax is:
git stash apply stash^{/stash_name}
For {/stash_name}
, specify the name or message assigned during stashing.
For example, to apply a stash named new_feature_introduced, run:
git stash pop stash^{/new_feature_introduced}
The command applies the saved changes but doesn't delete the stash from the ref.
2. Using Stash Index
Another way to retrieve a stash is to specify the exact stash index. Find a stash index by listing the existing stashes:
The syntax to restore a stash using the stash index is:
git stash apply stash@{n}
or
git stash pop stash@{n}
- For
{n}
, specify the stash index number. - Use the
apply
command to keep the stash in the ref after restoring it. Use thepop
command to delete the stash from the ref after retrieving changes.
For example, to pop the stash@{0}
changes and remove the stash from the ref, run:
git stash pop stash@{0}
The command restores the changes from the stash and schedules the stash for deletion.
Rename a Git Stash
To rename an existing Git stash, you need to drop the existing stash and create a new one using the old stash's SHA value. Git allows users to work with a dropped stash because it is only scheduled for deletion after running the drop
command.
Follow the steps below to rename a Git stash:
1. Find the stash you want to rename by listing existing stashes:
git stash list
2. Drop the stash by specifying its index number. For example, to drop stash@{1}
, run:
git stash drop stash@{1}
Make sure to copy the SHA value returned after dropping the stash.
3. Use the git stash store -m
command followed by the stash SHA value to add the stash again with a new message. For example:
git stash store -m "A bug fix" 863d76161bfd8d5269d1852879935648c2b27b45
The command adds the stashed changes back to the ref with the specified message.
Conclusion
This tutorial showed how to create a stash with a specific message and retrieve a Git stash using its name or stash index. You also learned how to rename an existing Git stash to provide a more detailed description. We suggest also checking out our guide on how to stash untracked files in Git using three different methods.
For more Git tutorials, see how to stash a specific file, or learn the basics of using Git stash.