

Git: how to use stash - robertp1984
http://softwarecave.org/2014/02/20/git-how-to-use-stash-2/

======
oneandoneis2
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"

~~~
adwf
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.

~~~
pornel
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.

------
pg_bot
Missing my favorite flag for stash: --include-untracked. This will also stash
any files that are not currently being tracked by git.

~~~
ddoolin
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.

~~~
fr0sty
> 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...

~~~
ddoolin
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.

------
wldlyinaccurate
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.

~~~
lmm
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.

~~~
saraid216
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.

~~~
ryanbrunner
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.

------
garrettlarson
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).

~~~
ozh
Making an alias to use something like `stash apply #3` shouldn't be too
complicated

~~~
heinrich5991
That's a bad idea, it would need quoting as the `#3` would be considered to be
a comment in most shells.

------
doognukem
I sometimes prefer using the git-wip commands.

[https://github.com/tjmehta/git-wip](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^

------
kooshball
My favorite stash option is -p which let you select which lines you want to
stash.

~~~
adsche
Yes, -p is great, also on `git add`.

~~~
J_Darnley
Also available on commit.

~~~
fr0sty
Don't forget reset and checkout.

Loads of fun.

------
VeejayRampay
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.

------
randallsquared
"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?

~~~
ndeine
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.

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

~~~
gamehead200
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.

------
dexen
Use stash for easier rebasing and/or pull --merge.

    
    
      git stash
      git pull --rebase origin master
      git stash pop

------
Trufa
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.

~~~
Myrmornis
Is that any different from cherry-picking it from a branch?

------
_kfb
It's worth pointing out that `git stash` (without arguments) is equivalent to
`git stash save`.

------
jimworm
This article made me git stash list and gee I hope I was faster doing all
those things the second time round.

------
zimbatm
I wish there was a better way to reference the stashes than "stash@{0}".

