How to Name a Stash and Retrieve a Stash by Name in Git

September 27, 2022

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.

How to name and retrieve a stash by name in Git.

Prerequisites

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 existing Git stashes.

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:

Creating a Git stash with a message.

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
Listing git stashes to check if the stash name appears.

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}
Applying a Git stash using regular expressions.

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:

Listing Git stashes to find the stash index.

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 the pop 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}
Popping a Git stash using the stash index.

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}
Dropping a Git stash.

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
Renaming an existing Git stash.

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.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
Git Tag: An Overview of the Basic Functions
September 6, 2022

This guide shows an overview of basic functions to perform with Git tags. See how to create, delete, push, replace, or checkout a Git tag.
Read more
Git Submodule Guide & Basic Commands to Get Started
September 1, 2022

This guide aims to provide an overview of Git submodules, focusing on the most frequently used commands and workflows.
Read more
Add, Update, and Remove Git Submodule
September 22, 2022

This tutorial will teach you to perform basic submodule management operations - adding, updating, and removing Git submodules.
Read more
How to Drop a Git Stash
September 14, 2022

A Git stash allows you to set aside unfinished work and later return to it, without having to commit the changes. This tutorial shows how to delete an existing stash.
Read more