Set a Git stash message. Pop with a Git stash name

How to retrieve a Git stash by a name

The ability to pop or apply a Git stash with a name is weakly supported in Git. There are plenty of corner cases that will fail if you attempt to use the Git stash message to apply a Git stash by a specific name.

However, the standard steps to pop a specific Git stash by name are as follows:

  1. Use the -m switch to add an entry Git stash with a message.
  2. Specify the specific stash to pop with stash^{/<stash-message>}.

How to set the Git stash message

To set the Git stash name, which is really just the message text associated with the stash, simply use the -m or –message switch:

### save a git stash with name
$ git stash push -m "stash-with-name"

How to apply a Git stash by name

To apply a specific Git stash by name, rather than specify the index of the stash, simply use a caret (^), also called a hat operator, and the Git stash name:

### perform a git stash apply by name
$ git stash apply stash^{/stash-with-name}

How to pop a Git stash with a name

To pop a Git stash by name, simply use pop instead of apply.

### perform a git stash apply by name
$ git stash pop stash^{/stash-with-name}

The content after the ^ is actually a regex expression, so be exact. Otherwise the command returns multiple results and the attempt to pop or apply a Git stash by name fails.

When to use the git stash command

Developers aren’t supposed to use the git stash command too often. It’s supposed to be reserved for those rare occasions such as the following:

  • You need to do a hard reset on your local working tree.
  • You don’t want to lose your current changes, which would result from a hard reset.
  • You don’t want to commit your local changes because that might break the build.

In situations like these, you commit your changes to a stash.

However, it’s easy to lose track of what’s been done in a particular stash, because it’s a seldom-used operation and stashes can hang around for a long time.

The default git stash name generation pattern isn’t helpful either, because it’s nothing more than a partial SHA sequence and a branch reference.

That’s why a developer should either use a git stash name or append a git stash message to describe what’s inside. Fortunately, there’s a simple way to do that.

How to list and show your Git stash history

A developer can add a message or descriptive git stash name with a -m switch appended and a text string in quotes:

/examples/apply git stash name (master)
$ git stash push -m "stash-with-name"

The next time you invoke the git stash list command it will be named, as in the example below:

/examples/apply git stash name (master)
$ git stash list
stash@{0}: On master: stash-with-name
stash@{1}: WIP on master: 6076134 scripting added
stash@{2}: WIP on master: cc49279 updated stylesheet

The git stash save by name option

Some old Git stash tutorials promote use of the Git stash’s save command.

The git stash save command is now deprecated. Use the push command instead.

However, you might need the save method for legacy systems and older shell scripts. The save command syntax differs from push in that you simply add a name as a text string at the end of the Git command without the -m switch:

/examples/apply git stash name (master)
$ git stash save stash-with-name

 

Developers report varying degrees of success when they perform a regex-based git stash apply by name. I have installed Git version 2.26 on Windows 10 and the apply command works brilliantly. Unfortunately, people who use older Git versions or run on Linux distributions aren’t always as lucky.

name git stash apply

How to pop and apply git stash by name.

The git-stash-with-name trick doesn’t work? Try the index

While it’s possible to pop a Git stash by name, I think it’s easier to just pop a stash by its index. In this example, I only have one entry in my stash history, so the command to pop or apply is as follows:

$ git stash pop 0
$ git stash apply 0

This is markedly easier to write than the regular expression that pops based on the git stash name.

As a best practice I definitely recommend that a developer name git stash entries to remind of the types of changes they contain. When you must locate an old stash, use the git stash list command to find out the index of the stash of interest. Instead of a git stash apply by name, just use the index. This strategy will work every time.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close