19 ms·
I use stash when I forget to branch. So I might be working and then realize I never branched. So I stash my changes, create a new branch, and then pop my change
by ryanSrich 10y ago
I use stash when I forget to branch. So I might be working and then realize I never branched. So I stash my changes, create a new branch, and then pop my changes on the new branch.
- dahart 10y agoMakes sense, but fwiw, there's a safer alternative: branch immediately, then go back to the source branch and reset. There's nothing wrong with your workflow, btw, stash works in that case, as long as you immediately pop & don't get interrupted by lunch or a phone call. Stashing is obviously providing some value that people like, it's very popular. But considering that it has extra danger, and that it's really easy to work without stash, I'm still confused why it's so popular. And fwiw, most of the stash accidents I've seen are fairly simple scenarios like you described, and some teeny tiny twist or interruption causes someone to get confused and lose their stash, or think they lost their stash, and panic.
- leephillips 10y agoI'm not a git expert (yet), but I agree. I've read the stash man page a few times and tried to imagine when I'd want to use it over the alternatives, but I never do.
- ben174 10y agoWhy stash? Why not just branch then commit?
- Grue3 10y agoBecause you might be currently not at the place where you intended to branch off. For example trying to make a bugfix on an experimental feature branch, but then you realize you should branch it off master.
- geofft 10y agoSo branch to a temporary branch, commit, checkout the branch you wanted to be on, cherry-pick the temporary branch to grab its head commit, then delete the temporary branch once you're sure you don't need it. This isn't completely safe because deleting a branch also deletes its reflogs, but it does avoid use of the stash command at all, which I find good for my own safety (because it reduces cognitive load). And it scales obviously to making multiple commits onto the temporary branch, etc.
- eridius 10y agoOr even just branch, commit, rebase onto the desired branch.
- ashark 10y agoMy #1 use of stash is as the quickest way to express "get rid of all this crap". I rarely intend to ever retrieve it.
- ianleeclark 10y agogit checkout -- . Also works well for this use case
- loeg 10y ago'git reset'.
- ashark 10y agoDoesn't do the same thing, without some more options. Stash is shorter.
- dahart 10y agoNow here's one I actually like. As an alternative to git reset, a git stash is safer, and gives you an easier way than reflog to recover if you dropped a commit you want back.
- spike021 10y agoFor one of my projects, the 'build process' requires that I use the git archive command (basically archive to .tar.gz without any git-related files/directories). git archive on its own is fine for final builds because it's based off of HEAD for the current branch, but if I want to test a build with working changes, I would either need to commit all the changes _or_ use git stash and then archive based on the state I just stashed. So in this case git stash works a lot better for a 'temporary' commit, kind of deal. It might not be optimal but I've found the process to work pretty well for this particular project's situation.
- spinningarrow 10y agoIf you're stashing your changes, that means they're just in the working copy and haven't been committed to the incorrect branch. In this case, simply create the branch and switch to it (git checkout -b branchname) and you're good. It's like it never happened ;) (remember that branches are just labels - until you commit it doesn't really matter).
- ryanSrich 10y agoTypically once I've noticed I haven't branched is after I've already added and committed.
- sammoth 10y agoThen just create the branch where you are, and afterwards go back and reset the original branch
- convolvatron 10y agoand make sure to patch up the upstream reference
- prashnts 10y agoI use `legit` aliases for this. It allows me to do `git sprout <new branch name>`, where it stashes my untracked changes, create a new branch, pop the stash and applies it. Pretty handy! legit: https://github.com/kennethreitz/legit https://github.com/kennethreitz/legit [1] [Ignore the pretentious "for humans" tagline though...]
- majewsky 10y ago`git sprout <branch-name>` looks identical to `git checkout -b <branch-name>`.
- mlvljr 10y agoWhy not branch some moment after realizing you forgot to and then go back to the (now gone a bit far) original branch and reset it to the original commit? Simple and does not require any arcane tricks.