If I am in a branch e.g. test
and have some work. If I do a git stash
and switch to master
branch, if I do a git stash clear
do I lose the work I saved in test
? Or does each branch have a separate stash stack?
Asked
Active
Viewed 1,129 times
1 Answers
11
No. Stashes are infact the reflog of a reference (sort of like a 'hidden branch', if you will): refs/stash
So,
git reflog refs/stash
e41a1b8 refs/stash@{0}: WIP on master: 42092ec PoC
is roughly equivalent to
git stash list
stash@{0}: WIP on master: 42092ec PoC
So there's only one "branch" containing all stashes.
The machinery might become more clear in this answer: Is it possible to push a git stash to a remote repository?
you can 'deduce' what branch the stash fits onto by doing, e.g.
git branch -a --contains stash@{0}^
(asking: what branches contain the parent revision for this stash?)
-
1As you suggested with the quotes around "branch" in the middle, `refs/stash` is not really a branch per se. (It *is* a reference, just as branches and tags are references; but so are notes.) `git stash branch ...` will let you change any given stash *into* a branch, though (by checking out the parent and then creating the new branch and applying the stash). So if you have a stash and decide it should become a branch, that's easy to do. – torek Oct 20 '13 at 21:26
-
@torek thanks for that addition. Would you believe I somehow always overlooked `git stash branch` :) Or perhaps my use of git predates that subcommand (?). Anyways, glad to have learned that. – sehe Oct 20 '13 at 21:30