Search code examples
gitgit-stash

How do I tell what branch I was on when I stashed changes?


I'm aware of git stash show -p stash@{0} giving the log of changes saved into that stash entry, but I'd like to know what branch I was on when I made those changes. This would be very helpful for a number of scenarios:

  • cleaning up my stash stack
  • when returning to an old issue (and therefore old branch) for which I stashed the changes, thinking at the time I would be able to soon return to the issue and unstash, but ended up being pulled onto some other issue(s) for a very long time. I find myself thinking "I think I stashed changes for this, but am unsure and if so, which entry in the stash stack were they."

Solution

  • That is shown with git stash list. In this example "on dude" shows branch dude and "on master" shows branch master.

    $ git stash list
    stash@{0}: WIP on dude: 7eb87fe initial
    stash@{1}: WIP on master: 7eb87fe initial
    

    Also, it sounds like you are stashing when you abandon work temporarily. I have another workflow that I think is superior to stashing, I commit it and then reset it. That way the change is on the branch and if I ever abandon that branch the related code is discarded as well.

    # do some work on dev
    git co -b dev
    # temporarily abandon it
    git commit -a -m'commit instead of stash'
    # do other work on master
    git co master
    # lets resume the old work
    git co dev
    # un stash
    git reset HEAD^