a repo with many branches might have one stash for each branch, ie, that many stashes, representing a wip on that branch. is there a way to switch to a branch and just apply the only stash for that branch, without having to select it first?
1 Answers
Stashes aren't on branches. (That's about half the point of a stash commit: since it's not on any branch, it is easy to use git stash
to "move" changes between branches. In the level at which git stash
works, branches are irrelevant—they don't even need to exist.)
What this really means is that there is no such one-to-one correspondence:
- It is true that any one given stash's default commit message is
WIP on branch
. - But you can have ten "WIP on master" and no "WIP on develop", or 2 "WIP on develop", one "WIP on master", and so on.
- And, you can give any stash a different commit message at the time you create it. If you're going to use
git stash
,1 this is probably a good idea.
All that aside, the short answer is no. The command probably should have an easier way to search git stash list
output, but at least as of now (Git 2.24) it does not. See How to name and retrieve a stash by name in git? for numerous ways to write your own searcher.
1I recommend not using git stash
very much. It's way too easy to create a lot of them and then lose track of them, and they don't do anything you cannot do by making a regular commit. Remember, all git stash
does is make some commits that are on no branch. Each stash consists of either two or three commits, that are made in a way such that git stash apply
can use them, but a lot of the rest of Git can't use them very well.

- 448,244
- 59
- 642
- 775
-
But a stash has (a) parent commit(s). It points to the commit from which it was created. If no further work has been done on the branch of the commit, the stash could be said to "be on that branch". So there might be a use-case for: check if the branch still points to the commit of the stash, checkout this branch/commit, apply the stash. – knittl Jan 03 '20 at 17:03
-
@knittl: yes, and that might also be something a fancied-up `git stash` could use to pick a stash: for each stash reflog entry, compare the result of `git rev-parse ${ref}^1` to a given branch tip. This could again be ambiguous, like the result of searching `refs/stash` reflog commit messages, so both code paths would need something to deal with that. – torek Jan 03 '20 at 17:08