sidnt
sidnt

Reputation: 300

When applying a git stash, how to automatically select and apply the only stash for the currently checked out branch?

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?

Upvotes: 1

Views: 161

Answers (1)

torek
torek

Reputation: 488193

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.

Upvotes: 1

Related Questions