News for hackers. Stuff that matters.
Re:Git: how to use stash by oneandoneis2 on Thursday February 20, 2014 @03:59AM (#7269735)
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"

Re:Git: how to use stash by Myrmornis on Thursday February 20, 2014 @05:46AM (#7270085)
Yes. One example use case for stash is saving some debugging change that makes sense being applied in many branches. But yes, most articles like this seem to be written by people who are yet to fully appreciate that branches are cheap/free.
Re:Git: how to use stash by foreigner on Thursday February 20, 2014 @04:31AM (#7269851)
Exactly right. When you first start using Git "stash" is a revelation, but over time you use it less and less. A good rule of thumb is "When in doubt, commit.". And embrace "rebase".
Re:Git: how to use stash by fr0sty on Thursday February 20, 2014 @09:23AM (#7270831)
Why would you recommend reset --soft ?

To get back to where you started (changes not added to the index, since you are doing commit -a) you should do `git reset --mixed HEAD~` (or drop the --mixed argument as it's the default).

Re:Git: how to use stash by adwf on Thursday February 20, 2014 @07:13AM (#7270353)
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 by pornel on Thursday February 20, 2014 @07:29AM (#7270405)
In git commits are done off-line, so nobody will see the commit if you don't push it.

You can clean up such work-in-progress commits before pushing by using interactive rebase.

Re:Git: how to use stash by taeric on Thursday February 20, 2014 @08:28AM (#7270606)
How is this really any better/different than just giving a message to a stash?
Re:Git: how to use stash by peterwwillis on Thursday February 20, 2014 @09:37AM (#7270905)
how many times have you stashed, and in what stages of development, and when you try to apply them later, will they be in order and will they work on the codebase as it has changed? also, do you even remember you stashed, and is there a log longer than 1 line?

stash is a quick hack, and is useful as a quick hack, but commits just work better.

Re:Git: how to use stash by taeric on Thursday February 20, 2014 @11:08AM (#7271505)
Still no different than branches you forget to go back to. If you want the exact same behavior, than instead of

    stash apply
use

    stash branch PickingBackUpStash 

That said, the majority of my uses of stash are "git stash; git pull; git stash apply" Or, alternatively, "git stash --keep-index; build; test; commit; git stash pop"

The whole point is I am not leaving the stash for long. If I have left a stash dormant for some time, I likely just drop it. Similar with branches.

Re:Git: how to use stash by pg_bot on Thursday February 20, 2014 @02:58AM (#7269533)
Missing my favorite flag for stash: --include-untracked. This will also stash any files that are not currently being tracked by git.
Re:Git: how to use stash by ddoolin on Thursday February 20, 2014 @09:02AM (#7270730)
git stash -u is much shorter :p This is default for me, now, since I've had untracked files straight up deleted because I didn't include them in my stash.
Re:Git: how to use stash by fr0sty on Thursday February 20, 2014 @09:36AM (#7270903)
> I've had untracked files straight up deleted because I didn't include them in my stash.

Do you recall the actual circumstances? That shouldn't happen in the normal course of things...

Re:Git: how to use stash by ddoolin on Thursday February 20, 2014 @01:27PM (#7272625)
It was an awkward circumstance where I'm sure I majorly screwed up. The problem is that I can't remember...which is why it's my default....just in case. :)

All I know is there was some stash dropping and checking out involved and somehow I lost a couple day's worth of work...oops.

Re:Git: how to use stash by wldlyinaccurate on Thursday February 20, 2014 @03:15AM (#7269603)
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.
Re:Git: how to use stash by lmm on Thursday February 20, 2014 @05:47AM (#7270091)
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 by saraid216 on Thursday February 20, 2014 @05:54AM (#7270113)
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 by ryanbrunner on Thursday February 20, 2014 @06:33AM (#7270241)
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 by lmm on Thursday February 20, 2014 @06:17AM (#7270191)
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 by garrettlarson on Thursday February 20, 2014 @02:20AM (#7269399)
Does anyone know why stash names are so ugly? I've always found typing "git stash apply stash@{3}" to be pretty painful (not to mention redundant).
Re:Git: how to use stash by _kfb on Thursday February 20, 2014 @03:47AM (#7269692)
You might want to check out the Bash completions for Git: https://github.com/git/git/blob/master/contrib/completion/gi.... They work with stash names so can take some of the pain away.
Re:Git: how to use stash by pornel on Thursday February 20, 2014 @07:33AM (#7270412)
It's a generic git syntax for "this reference 3 versions ago", see `man git-rev-parse`

It works for other things too, e.g. you can undo rebase with `git reset master@{1}` (i.e. reset master to previous location of master).

Re:Git: how to use stash by ozh on Thursday February 20, 2014 @03:42AM (#7269678)
Making an alias to use something like `stash apply #3` shouldn't be too complicated
Re:Git: how to use stash by heinrich5991 on Thursday February 20, 2014 @05:08AM (#7269982)
That's a bad idea, it would need quoting as the `#3` would be considered to be a comment in most shells.
Re:Git: how to use stash by doognukem on Thursday February 20, 2014 @02:33AM (#7269442)
I sometimes prefer using the git-wip commands.

https://github.com/tjmehta/git-wip

Let's me organize my "stashes" based on work-in-progress local branch commits. Also easier to rebase onto if it's a local branch that hasn't been pushed yet etc.

  git wip 
    - alias for git add .; git commit -m __wip;

  git unwip 
    - checks if last commit is a __wip and git reset HEAD^
Re:Git: how to use stash by kooshball on Thursday February 20, 2014 @03:11AM (#7269584)
My favorite stash option is -p which let you select which lines you want to stash.
Re:Git: how to use stash by adsche on Thursday February 20, 2014 @03:29AM (#7269645)
Yes, -p is great, also on `git add`.
Re:Git: how to use stash by J_Darnley on Thursday February 20, 2014 @06:03AM (#7270146)
Also available on commit.
Re:Git: how to use stash by fr0sty on Thursday February 20, 2014 @09:39AM (#7270921)
Don't forget reset and checkout.

Loads of fun.

Re:Git: how to use stash by VeejayRampay on Thursday February 20, 2014 @03:55AM (#7269724)
I never stash. I just commit instead and then rewrite my commit history with some reword/squash/fixup using git rebase -i. So in the end, it's like that half-commit never existed and I'm good to go. I've also found that people tend to forget about stashes. I can't count the times where I've found people with 3 or 4 months-old stashes swept under the rug.
Re:Git: how to use stash by randallsquared on Thursday February 20, 2014 @07:27AM (#7270396)
"Normally, I would have to save the changes (diff) into some file, switch to the main branch abandoning any changes, apply the fix or improvement and commit it. Then I could switch back to my own branch, apply the changes (patch) from the file and continue the work."

Really? Why would you "normally" jump through all these hoops just to work in the same directory? Using svn, my response to this is to jump back to the root of all my projects (~/codebase/ at my current work), checkout a clean version of the branch I need to work on, make changes / test / commit, and rm -rf the whole branch, changing directory back to where I was. If I get blocked on that change for a few hours, I don't have to jump through additional hoops to go back to working on the thing that was interrupted, since I can just change directory back to it.

Is there a reason why this is an awful workflow in git?

Re:Git: how to use stash by ndeine on Thursday February 20, 2014 @07:48AM (#7270471)
He's not actually talking about saving the diff and patching within a git repo, as far as I can gather from:

> [...], apply the changes (patch) from the file and continue the work. While it is not something difficult, it can be done much easier with Git.

Since he's saying "it can be done much easier with Git", the implication is that he wasn't using Git earlier, and the workflow with `git stash` is far cleaner than that diff/patch workflow.

At least, I think that's the case. I really can't imagine someone doing that instead of just using Git and `git stash` in the first place.

Re:Git: how to use stash by Eiwatah4 on Thursday February 20, 2014 @07:56AM (#7270487)
That works just fine. But if you're working on a big project, a full new download can take a while.
Re:Git: how to use stash by sauronlord on Thursday February 20, 2014 @02:44AM (#7269470)
Curious.... why not use feature branches for absolutely everything and follow the git flow and treat hot fixes as prescribed?
Re:Git: how to use stash by gamehead200 on Thursday February 20, 2014 @03:34AM (#7269658)
Depends on what you're working on, really. I find myself using stash when I quickly want to undo a change to check something, but preserve it for future use.
Re:Git: how to use stash by Dirlewanger on Thursday February 20, 2014 @07:21AM (#7270376)
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.
Re:Git: how to use stash by emillon on Thursday February 20, 2014 @06:03AM (#7270148)
For example, to test partial commits:

    git add -p
    git stash
    test
    commit
    git stash pop
Re:Git: how to use stash by dexen on Thursday February 20, 2014 @04:15AM (#7269790)
Use stash for easier rebasing and/or pull --merge.

  git stash
  git pull --rebase origin master
  git stash pop
Re:Git: how to use stash by Trufa on Thursday February 20, 2014 @05:11AM (#7269990)
The most useful part was the comment:

> I think it’s worth emphasising that stash stores just your changes and not a snapshot of the entire tree. This means that you can create your stash on one branch and then pop it onto another branch. When you pop from the stash a merge is done including any conflict resolution that may be necessary.

Re:Git: how to use stash by Myrmornis on Thursday February 20, 2014 @05:42AM (#7270075)
Is that any different from cherry-picking it from a branch?
Re:Git: how to use stash by _kfb on Thursday February 20, 2014 @03:48AM (#7269696)
It's worth pointing out that `git stash` (without arguments) is equivalent to `git stash save`.
Re:Git: how to use stash by jimworm on Thursday February 20, 2014 @07:24AM (#7270385)
This article made me git stash list and gee I hope I was faster doing all those things the second time round.
Re:Git: how to use stash by zimbatm on Thursday February 20, 2014 @07:16AM (#7270363)
I wish there was a better way to reference the stashes than "stash@{0}".