Yes, it's useful to quickly stash your work, switch to a different branch, do some quick fix, then go back to your original branch.
But in practice, what often happens is that your quick fix takes way longer than expected, and you don't immediately go back to your original branch.
Then you pull in some changes, maybe someone else pushed something to the branch you were working on, and you end up with a bunch of stashes on outdated branches and it's really hard to make sense of it all. Most of my working copies have at least half a dozen old stashes that I don't remember any details about anymore.
One approach to avoid this mess that git stash can lead to is to never use it, and just commit all your work before switching to a different branch. Clean up the current state of the repo, try to document your changes in the commit message, and commit them. Then switch branches.
You can always squash or rewrite the commits later, and it's a lot easier rebasing or merging a few old commits than working with stashes.
If you like stashes, please use them!
But I agree with the author, using them a lot can lead to a mess, and temporary branches are usually a cleaner way of saving your work.
I had to resort to git's assume-unchanged[O] to keep these changes out of commits made in the heat of battle.
> this isn't a best practice for config files
Have had great results (in dotnet land) with MS' Secrets Manager[1].
[0] https://www.git-scm.com/docs/git-update-index [1] https://docs.microsoft.com/en-us/aspnet/core/security/app-se...
The technique is not applicable all the time, but for those instances when you really think it's valuable to preserve the knowledge that was gained in trying and abandoning a particular approach it's great.
git add .
git commit -m "wip"
when you're back git reset HEAD^
Keeps working changes nice and tidy in the branch they belong to, you can push them, and they are easily recoverable from the ref log, unlike stashes.Also, god help you when a stash fails to apply.
But this workflow (which seems so very obvious in retrospect), keeps my current workflow, with so many benefits. The only place where it even requires extra steps is when you want to apply the stash to a different branch, and even there, it just feels right:
“Oh yeah, I was working on that feature over there, let’s pop it off and go put it on this feature over here.”
Since this generally requires careful diffing anyway to make sure things end up in the right place, there’s not much lost here, and without the downsides of stash.
Each time you run `wip`, all current work is committed. When you run the squashing utility `naenae` (autocomplete: `nae + Tab`), the most recent line of "WIP" commits is saved to a `wip-archives/[branch]/[timestamp]` branch for safe keeping, the commits are all squashed in place, and you're left in your default git editor to define the commit message. It also handles unusual situations, like using `wip` for the first commit (in which case, `git reset HEAD^` will return an error).
Being able to set a sort of "WIP checkpoint" in the current working directory makes it really easy to start or back out of big changes, and it can be very useful to check back in past WIP commits to grab segments of code you previously threw away.
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.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.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.
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.
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.
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
They are for short terms manipulations, but it's easy to make mistakes with them and lose data (dropped stashes don't show up in the reflog for instance).
Things to be preserved for longer should be committed and saved in temporary branches.
Why is stash better than this? Because it avoids the rebase? Many people do this anyway, early commit messages often need rewording. Because it maintains the separate state of the current working directory and the index? Is that it? I don't think that's really a compelling enough reason to claim the author's avoidance of stash is the wrong way to work.
Like the article writer, I never myself use stash. I personally don't understand why they were added to the git ux. I think they needlessly make git more complex. Under the hood, a stash is just a commit anyway. You might retort that this is an implementation detail but as usual with Git, this leaks into the way it is used: if you ever lose a stash by mistake, you will have to go fish the corresponding commit in the reflog.
So I can `git pull --rebase --autostash` and this works even with local modifications.
Perhaps a Mercurial user can say if shelves are more useful? (And if so, why?).
One "right tool for the job" I really underappreciated until recently is git worktree, which fits this use case neatly, I think, without the risk of losing your stash.
Er, no. Right tool for the right job.
Yup. pull --rebase --autostash for example. Technically also stashing, and would be insane not to use it :)
More serious note: agreed. I use stashing multiple times a day and never have problems with things being messy. Though I admit I usually look t stashed in a gui, not from the terminal.
"One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision."
-Bertrand Russel
Er, no. Stash does not return to a clean working state. Stash stashes modifications, not new files. And it makes a real mess if those new files happen to exist on another branch you want to check out.
Stash seems like a half-implemented feature tbh, if only for the infuriating limitation above. It's really just not safe to use.
https://www.atlassian.com/git/tutorials/saving-changes/git-s...
I’d be willing to bet they sometimes, even if rarely, use stash when as you say it’s the right tool to use.
I think the point is not stashing meaningful amounts of changes, as if they can be recalled later. Sounds like solid advice that requires some discipline
> right tool for the right job
I'll give you that this is (slightly) ambigious. But here's the gist
>you can safely ignore the opinion of anyone who says "never do X"
There are lots of alternative tools listed in this comment tree.
Stash has a purpose. You should know what that purpose is. A tool is only as good as its user
Er, no. Right tool for the right job.
Whilst I agree that the idea of putting differing lines of work into different branches is great, that's not what stash is for. That's what checkout is for.
Stash is used to record the current state of the working directory and index while also returning to a clean working state.
I guess for the author, stash is used to preserve changes when pulling in upstream changes you missed. It can, but it does waaaay more.
What happens if, like many people, you're working on a few branches at a time. Could be a monorepo, could be a couple long living branches, w/e. What do you do if you've done work against a branch and want to lift and shift to another branch?
You could commit and rebase and all sorts of clever mangling of the history.
OR you could use the right tool for the job
---
Follow up
[0] https://medium.com/@yankee.exe/mastering-git-stash-workflow-...
[1] https://git-scm.com/book/fa/v2/Git-Tools-Stashing-and-Cleani...