4

I have 2 branches: Section9 and s9feature. On the branch s9feature I made stash, then I checkout on Section9 branch and want to see the list of stashes specifically from s9feature. I know that I can run git stash list or git reflog stash but I need to list out stashes specifically from s9feature

Katerina
  • 43
  • 4

2 Answers2

5

Stashes are based on commits, not branches. However, the default "title" of a stash—which is actually just its commit message, as each stash is just a commit that's not on any branch1—has the form WIP on branch. So, you can run git stash list, and then use a filter on its output to extract any line containing the branch name. For instance:

git stash list | grep s9feat

(remember that grep searches for any substring, so as long as s9feat is long enough to distinguish the interesting stashes from the uninteresting ones, that's all we need here).

If you've changed the titles of the stashes, of course, this won't work. Since branch names are meaningless and irrelevant to Git, and only appear in the human-oriented message part, you'd need something considerably more complicated to find the interesting stashes—unless, that is, you already put the interesting part into these changed titles.


1Technically, each stash is at least two commits. Stashes made with particular options add a third commit to hold untracked files.

torek
  • 448,244
  • 59
  • 642
  • 775
0

However, the default "title" of a stash has the form WIP on branch

The problem is: it does not display the full name of the branch it was created on. Not before Git 2.36 (Q2 2022)

"git checkout -b"(man) branch/with/multi/level/name && git stash(man) only recorded the last level component of the branch name, which has been corrected with Git 2.36 (Q2 2022).

See commit ceaf037 (24 Jan 2022) by Glen Choo (chooglen).
(Merged by Junio C Hamano -- gitster -- in commit 11da0a5, 06 Mar 2022)

stash: strip "refs/heads/" with skip_prefix

Reported-by: Kraymer
Reported-by: Daniel Hahler
Helped-by: Jeff King
Signed-off-by: Glen Choo

When generating a message for a stash, "git stash"(man) only records the part of the branch name to the right of the last /".
e.g.
if HEAD is at "foo/bar/baz", "git stash" generates a message prefixed with WIP on baz: instead of WIP on foo/bar/baz:.

Fix this by using skip_prefix() to skip "refs/heads/" instead of looking for the last instance of "/".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250