Live data from Hacker News

Git: how to use stash

softwarecave.org

21–30 of 43 posts

Re: Git: how to use stash

#21

I hardly use stash any more. If I need to "stash" something, I just commit it. The reason this is okay is because we work on very small branches, with the aim of squashing everything into a single commit before merging it with master. So it doesn't matter if I commit half-baked code to my branch, since it's going to be squashed anyway.

I find that scenario makes me use stash much more - I usually have several branches on the go, so I can sometimes make changes while on the wrong one, at which point I'll stash and switch to the correct branch.

Re: Git: how to use stash

#22
post #21

I hardly use stash any more. If I need to "stash" something, I just commit it. The reason this is okay is because we work on very small branches, with the aim of squashing everything into a single commit before merging it with master. So it doesn't matter if I commit half-baked code to my branch, since it's going to be squashed anyway.

I find that scenario makes me use stash much more - I usually have several branches on the go, so I can sometimes make changes while on the wrong one, at which point I'll stash and switch to the correct branch.

Huh. It's worth noting that you don't have to have a clean branch before you switch to another. You'll automatically shunt the changes to the new branch when you switch.

I've often had the workflow of "make a bunch of changes on master; git checkout -b new_feature_branch; git commit -am 'save it off'; git checkout master".

I haven't looked into the plumbing that goes into that, but I suspect it's actually stashing and popping for you.

Re: Git: how to use stash

#25
post #21

Earlier quoted context omitted.

I find that scenario makes me use stash much more - I usually have several branches on the go, so I can sometimes make changes while on the wrong one, at which point I'll stash and switch to the correct branch.

Huh. It's worth noting that you don't have to have a clean branch before you switch to another. You'll automatically shunt the changes to the new branch when you switch. I've often had the workflow of "make a bunch of changes on master; git checkout -b new_feature_branch; git commit -am 'save it off'; git checkout master". I haven't looked into the plumbing that goes into that, but I suspect it's actually stashing an…

Yeah, most of the time that works fine, but if my version of the branch I'm switching onto is behind then switching can induce "fake" conflicts; it's easier (I find) to just be in the habit of always stashing.

Re: Git: how to use stash

#26
post #21

Earlier quoted context omitted.

I find that scenario makes me use stash much more - I usually have several branches on the go, so I can sometimes make changes while on the wrong one, at which point I'll stash and switch to the correct branch.

Huh. It's worth noting that you don't have to have a clean branch before you switch to another. You'll automatically shunt the changes to the new branch when you switch. I've often had the workflow of "make a bunch of changes on master; git checkout -b new_feature_branch; git commit -am 'save it off'; git checkout master". I haven't looked into the plumbing that goes into that, but I suspect it's actually stashing an…

This is only true if one of the files that you've changed has 100% no changes between the branch you're currently on and the branch you're switching to. Even stuff that would normally be auto-merged will not allow you to switch branches.

I still usually give it a try, and then stash when that gives me a problem. With 12 developers and some high-churn files, it's pretty common that 2 people are doing something with the same file, even with branches that only last 1-2 days.

Re: Git: how to use stash

#27

Far better: git commit -a -m 'Work in progress' Switch to other branch, do work on it. When you come back to the branch, git reset --soft HEAD^ It's far less volatile than stash, and far better at keeping WIP in the right place when things get so hectic that you have abandoned work on more than one branch. Stash is only really safe to use for very short-lived uses, such as "stash/pull/unstash"

I think it's because some workplaces have policies about not committing partially done code. ie. Don't break the build.

Personally, if I'm on a local branch I don't care, but I guess it might bother some people.

Re: Git: how to use stash

#29

Curious.... why not use feature branches for absolutely everything and follow the git flow and treat hot fixes as prescribed?

Stash is invaluable for when I need to do a hotfix or something or other and the work I'm currently doing on a feature branch is a mess of half-completed files that I just would not feel comfortable calling an actual commit. Stash allows you to save this and then come right back to it. Not every workflow needs to be followed to a T, at all times.
Post reply on HN