A git stash name: Easily pop or apply changes by name example

How to pop the Git stash by name

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

  • You need to do a hard reset on your local working tree;
  • You don’t want to lose your current changes (which would be the result of resetting hard); or
  • 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.

<tl;dr>
### save a git stash with name
$ git stash push -m "say-my-name"

### perform a git stash apply by name
$ git stash apply stash^{/say-my-name}
</tl;dr>

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 create a git stash with a name

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

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

The next time the git stash list is invoked, it will have been named, as in the example below:

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

The git stash save name option

The ‘git save’ command is now deprecated, and the ‘push’ command is preferred. However, the save method might be needed 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 say-my-name

How to perform a git stash apply by name

The trick to apply a git stash by name is to use some regex in the command. The general syntax is as follows:

$ git stash apply stash^{/<regex>}

The command to apply a git stash named “say-my-name” would be:

/examples/apply git stash name (master)
$ git stash apply stash^{/say-my-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.

When the git stash name doesn’t work…

Having said that, 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:

$ git stash pop 0
$ git stash apply 0

I’d find this markedly easier to write than the regular expression that pops based on the git stash name.

What do I recommend as a best practice? Definitely name git stash entries to remind you of the types of changes they contain. When you need to 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. It’s a strategy that will work every time.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close