Home > Enterprise >  `git stash list` doesn't show the before-the-slash part of branch names
`git stash list` doesn't show the before-the-slash part of branch names

Time:05-25

I have a post-checkout hook that uses git stash list to see if there's a stash in the current branch, and tells the user if there is one.

It's failing when the branch has a slash in it, like feature/footest, because the message created by 'git stash' doesn't include the feature/ part:

$ git status
On branch feature/footest
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   config/locales/en.yml

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash
Saved working directory and index state WIP on footest: 5a81e44c1f Couple more

So, the message that's automatically created by git stash is

WIP on footest: 5a81e44c1f Couple more

Then, later, my code checks that message (when output from git stash list) to see if the branch matches the current branch, and decides that it doesn't, because "feature/footest" doesn't equal "footest".

I don't want to assume that "feature/footest" and "footest" are the same thing. Ideally I'd like git stash to change its automatically generated name for the stash to include the full branch name. Is this possible?

I'm using git version 2.34.1 on macos.

EDIT 1

  • answer to @j6t's comment:
$ git show-ref | grep footest
5a81e44c1ff2930f02047d17f75364d8a8d0e20f refs/heads/feature/footest

CodePudding user response:

The answer to this was simply to upgrade git. I'm now on v2.36.1 and the automatically generated stash message now shows the full branch name.

BTW in case anyone's interested this is my post checkout hook that tells me if I have stashed changes in the branch. It's really useful for an absent-minded programmer like myself.

$ cat .git/hooks/post-checkout
#!/bin/sh
branch=$(git rev-parse --abbrev-ref HEAD)
stashes=`git stash list | grep "WIP on $branch"`
if [ "$stashes" ]
then
  echo "You have the following stashes for this branch:"
  echo $stashes
fi

CodePudding user response:

This was a bug added during the conversion of git stash from shell script to C code. The bug was present in Git versions 2.22 through 2.35 (fixed in 2.36.0; see commit ceaf037f617eb774bb8a451c1779dd9b8b12152a). Note that the only effect of the bug is to represent "multilevel" branch names incorrectly in the stash's default title / subject line. That is, stashing while on branch fie says WIP on fie but stashing on foo/bar/baz generated WIP on baz instead of WIP on foo/bar/baz.

  •  Tags:  
  • git
  • Related