4 ms·
> You could commit and rebase and all sorts of clever mangling of the history. > OR you could use the right tool for the job Stashes are just another form of D
by MaulingMonkey 6y ago
> You could commit and rebase and all sorts of clever mangling of the history.
> OR you could use the right tool for the job
Stashes are just another form of DAG node with their own special syntax and commands and quirks. I've already learned one set of commands for all that, why learn a second, less general set of commands?
I have started using stashes in one very specific case:
1. I realize I'm on the wrong branch
2. I've made no commits
3. The right branch has changes to files I've modified
git stash push
git checkout -b newbranch upstream/right
git stash apply
...verify...
git stash list
git stash drop [id]
Careful and safe, but not actually much faster than my old workflow, and I haven't the foggiest idea how to fix things up with stash if I've made several commits, if that's even a thing.
git checkout -b dead/wrong
git add -A .
git commit -m "WIP"
git checkout -b newbranch
git rebase --onto upstream/right upstream/wrong
...verify...
# optional
# git branch -D dead/wrong
# git reset --soft HEAD~1
Depending on if you count the optional commands, that's one more or one less command. "Those aren't optional!" you might say - but yes, they are. I'll often merely amend my WIP commit into a real one instead of resetting. I'll often keep dead branches long term - perhaps some of my work relied on things that haven't landed on upstream/right yet, and can't be brought over sanely until upstream/wrong lands on upstream/right, or just for peace of mind that I might've forgotten something. Good dead branch names makes this less painful than... numbered stashes? Eww, gross.
- IggleSniggle 6y agoI usually do comment my stashes for this reason (git stash save “words to remind me what this is”). But I’ve also been convinced by comments here that I’m going to be happier with WIP commits. I like making all my commits “canonical at all times,” but that’s just something I can see it’s time for me to evolve beyond.
- theamk 6y agostash is supposed to be a short term thing, if you are doing the whole "apply/verify/list/drop" cycle, named branches are a superior approach. I find the power of the stash in "stash pop", which applies, and drops if there were no conflicts: git stash push git checkout -b newbranch upstream/right git stash pop and yeah, if your keep your branches long-term stash is not a good solution for this.
- wutbrodo 6y agoYea, exactly. The author's git workflow is eerily like the one I've converged too over a decade, but "never stash" (without acknowledging the existence of the quick stash use-case) is wrong enough that it's enough to give me pause about his general competency with git (or more likely and more charitably, that he just didn't put too much effort into that section of the article). The advantage of stash is that it's quicker and easier than creating and cleaning up branches. If you're naming stashes and keeping them longterm, then you're basically using them like branches and should consider what benefit you're getting from adding a separate workflow.
- MaulingMonkey 6y agoA lot of my "short term" things end up being multi-month distractions or worse - and the verify/list/drop cycle is reflexive paranoia after one too many botched merges lost data. Sometimes those botched mergeds have been due to my own incompetence, sometimes due to P4Merge literally corrupting my file and saving out something totally different than what I'd resolved, and sometimes due to unexpected refactoring between branches causing enough of a mess that reapplying the work by hand is easier than "resolving" the conflict.
- smokey_circles 6y agoGood points all round. But what I'm not doing is saying "you should always use stash". I'm providing use cases, for me personally, where stash is the correct approach. "Ah", but you might be inclined to argue, "such and such a reason". Yes, but also no. Right tool for the right job is also an argument for competency and context. Sometimes a hammer is useful for more than just hammering, but very rarely. The point is there is usually more than just 1 approach, and niche/edge cases can be real nasty if approached with the attitude that everything is a nail