85

I suppose it allows for moving changes from one branch to the next but that's what cherry picking is for and if you're not making a commit of your changes, perhaps you shouldn't be moving them around?

I have on occasion applied the wrong stash at the wrong branch, which left me wondering about this question.

Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
iros
  • 1,055
  • 1
  • 7
  • 8
  • 13
    Git is missing something which allows you to associate uncommitted changes with a branch (or head). This becomes apparent when you have work in progress for more then one branch at a time. As the answers here point out, stash has useful applications based on its characteristics nonetheless. `git stash-here` anyone? – Blake Taylor Oct 04 '11 at 19:53

4 Answers4

69

As mentioned, if you want a “per-branch stash,” you really want a new branch forking off from the existing branch.

Also, besides the already mentioned fact that the stash allows you to pull into a branch that you’re working on, it also allows you to switch branches before you have committed everything. This is useful not for cherry-picking in the usual sense so much as for cherry-picking your working copy.

F.ex., while working on a feature branch, I will often notice minor bugs or cosmetic impurities in the code that aren’t relevant to that branch. Well, I just fix those right away. When time comes to commit, I selectively commit the relevant changes but not the fixes and cosmetics. Instead I stash those, which allows me to switch to my minor-fixes-on-stable branch, where I can then apply the stash and commit each minor fix separately. (Depending on the changes in question, I will also stash some of them yet again, to switch to a different feature branch, where I apply those.)

This allows me to go deep into programming mode when I am working, and not worry about proper librarianship of my code. Then when I take a mental break, I can go back and carefully sort my changes into all the right shelves.

If the stash weren’t global, this type of workflow would be far more difficult to do.

Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
  • 5
    I don't agree with this: `As mentioned, if you want a “per-branch stash,” you really want a new branch forking off from the existing branch.`. In particular, I often switch between two versions of the same program, and running a full `make` takes 10 minutes; being able to stash the built binaries, per-branch, would be very nice. And I certainly don't want the binaries in the log or in a branch. – Clément May 07 '15 at 00:53
  • Why? (I think you do, you just don’t know it.) – Aristotle Pagaltzis Jun 21 '15 at 22:22
21

As of Git 1.6, you can now apply stashes to branches using

git stash branch name_of_new_branch

Git will create the new branch for you, and check it out! For more information, see

I'm guessing you can move stashes around using

git stash branch <branch | new_branch> [<stash>]

and to see a list of your stashes, use

git stash list

Reference

TomRoche
  • 1,464
  • 1
  • 16
  • 25
Igbanam
  • 5,904
  • 5
  • 44
  • 68
  • Thank you! I wasn't aware that you could apply stashes to branches, but this appears to be the behavior after all. Having a stash on one branch I checked out another branch and did some work there. As it turned out I needed to stash some of its pending changes also before I could rebase to master. My updated "`gitk --all`" no longer showed my first stash; I thought it might be clobbered. After my work in the second branch was done I then popped that stash. After that, my updated "`gitk --all`" showed the original stash again. – Daniel Miladinov Feb 05 '13 at 22:22
17

if you want a "stash" that runs off a branch do something like this to store your changes on a new branch off your current branch.

git checkout -b new_stash
git commit -a -m "stashed changes"

to undo the stash

git reset HEAD^
git branch -d new_stash

git stash is especially useful because you can pull changes into a dirty tree, i.e. if you have outstanding edits and want to do a

git pull

and you can't, you can stash your changes, pull and then apply the stash

git stash
git pull
git stash apply
git stash clear

hope this helped!

Patrick_O
  • 760
  • 5
  • 13
  • What if the stuff you're trying to stash is, for example, a bunch of binaries that resulted from running `make` in that branch? (a per-branch stash would save time next time you do an incremental build in that branch, but you certainly don't want to add the binaries to the index) – Clément May 07 '15 at 00:54
5

git-stash is most useful to me to move not-yet-checked-in changes off to a different branch than the one that is currently checked out.

For example - I often find myself doing simple changes on a bug-fixes branch; only to find that a change I'm working on is more complex than I first guessed. Git-stash is the easiest way to move that set of changes to a different branch.