One place where the stash is really useful is pre-rebase. Unlike in merge, you need a clean worktree before you can rebase irrespective of whether or not those worktree changes will be affected by the rebase. A very common workflow on a topic branch:
$ git stash
$ git rebase master
$ git stash pop
To automate this workflow, there is a topic called rr/rebase-autostash that introduces the configuration variable rebase.autostash which will automatically 'stash' and 'stash pop' for you.
The topic is still cooking in git.git, and is due to hit `master` in a few days. If you want to know more, read about it from the `pu` branch of git.git. I've put up the most important commit message here [1] for convenience.
Useful post that taught me a few new tricks about stash.
I like to use stashes as opposed to a "half commit" mentioned by others because conceptually it's not a commit; it represents a chunk of work that is not in a commit-ready state, but is a work in progress. You can (and almost always should, IMHO) include a description of a stash and you also know what commit the stash is based off on and the branch you had checked out when you stashed. Thus, you get the same basic information as a "half commit" while avoiding what for me is a mixing of conceptually different things that I prefer to keep separate.
Of course, to each their own. That's one of the things I love about git is that it offers at least some flexibility to taylor it to what you feel is your optimal workflow.
git-stash is cool, but it can be easy to fall into a habit of creating stashes that you never come back to. When you finally list them all, you find 10, 20 stashes, and you can't remember what they all refer to. I find that situation nightmarish.
I prefer to make a half-commit (and note this), and simply rebase -i later on (before I push my feature branch).
Then again, I'm very comfortable with rebasing my work and how to avoid issues when doing so, and I recognize that not everyone is.
I have my stashes listed in the status, and drop them when needed. For me committing requires a good name and needs to be "somewhere", so it is a deeper action, while I can put any kind of crap in a stash instantly, and drop it if unused.
stash is something that should be used for only the briefest of times - the only time I use it is "stash, pull, unstash"
If you want to save partial work to a branch, commit it, go do your work on the other branch, then use "reset --soft HEAD^" to undo the commit but keep the work.
will show you what branch was checked out when you stashed. Also, because a stash is represented as a commit, you can know what commit the stash is based on. From the git-stash man page:
"A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created. The tree of the second parent records the state of the index when the stash is made, and it is made a child of the HEAD commit. The ancestry graph looks like this:
.----W
/ /
-----H----I
where H is the HEAD commit, I is a commit that records the state of the index, and W is a commit that records the state of the working tree."
The topic is still cooking in git.git, and is due to hit `master` in a few days. If you want to know more, read about it from the `pu` branch of git.git. I've put up the most important commit message here [1] for convenience.
[1]: http://artagnon.com/git-rebase.autostash