3 ms·
Do people really use stash for things they value longer than a few minutes? I've only ever used stash as a way to store changes that I made on the wrong branch
by rplst8 10y ago
Do people really use stash for things they value longer than a few minutes?
I've only ever used stash as a way to store changes that I made on the wrong branch.
git checkout my_old_branch (wrong branch not realized yet)
- work work work...
git status
- "oh, I'm on the wrong branch"
git stash
git checkout the_correct_branch
git pull
git stash apply (or git pop - but you lose what was on the top of the stash)
- dahart 10y ago> Do people really use stash for things they value longer than a few minutes? You'd be surprised. ;) Even a few minutes can get you into trouble though. The very last person I helped out of a stash crisis two days ago did exactly what you outlined, fully intending to stash pop immediately, and couldn't find his change and panicked. What actually happened was he'd unknowingly stashed again during his branch switch, and his first stash was there, but not on the top of the stack. GitHub desktop was auto-stashing or something. (GitHub desktop has made several very strange choices...) Anyway when my friend applied an empty stash, he thought he'd lost his changes. He called for help on a Saturday, and I tried to point him in the right direction over email, but by the time I got to a terminal to see what happened, he'd already rewritten most of his change, having decided it was a loss. OK, FWIW here are some safer alternatives to switching branches without stash: - First thing to know is that you can switch branches with uncommitted changes in your working tree. You don't have to do anything else, git will protect against conflicts, so why not always try this first? It's less work most of the time. If git complains, you can: git commit -am "WIP for the_correct_branch" git checkout the_correct_branch git cherry-pick my_old_branch git reset --soft HEAD^ # Optional, if you want to unstage ... Later ... git checkout my_old_branch git log ... Notice the leftover commit ... git reset --hard HEAD^ Or here's an alternative that doesn't leave the dangling commit for later: git commit -am "WIP" git branch work # you can always do this with uncommitted changes git reset --hard HEAD^ # still on my_old_branch here git checkout the_correct_branch git cherry-pick work git branch -d work If anything goes wrong with these workflows, my changes have been dropped in two branches, they'll be easy to find in the reflog, and the gc timer doesn't even start yet because they're still referenced. It's really hard to lose the changes this way. The only option with the stash flow if the stash gets lost while switching branches is to use fsck, and the gc timer starts immediately because the stash is not referenced. I mention the gc timer because some of the accidents I've seen are people popping stashes and only realizing later that they popped a different change than they thought they did. They way this happens is people who are beginners to git. Once you know git, it's hard to imagine popping the wrong stash and not knowing it, but I've seen it happen multiple times. This is why I advise everyone to avoid stash - for beginners, it looks deceptively and attractively simple, but is easy to get in hot water. It's also another git subsystem to learn, as if git wasn't hard enough to learn. For git experts, it's so easy to avoid using stash, there are always safer alternatives.