Search code examples
gitgit-stashgit-refloggitlist

Git list out stashes specifically from one branch


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


Solution

  • 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.