<p>In a <a href="https://stackoverflow.com/questions/21848/switch-branch-names-in-git">previous Git question</a>, Daniel Benamy was talking about a workflow in Git:</p>

<blockquote>
  <p>I was working on master and committed some stuff and then decided I wanted to put that work on hold. I backed up a few commits and then branched from before I started my crap work.</p>
</blockquote>

<p>He wanted to restore his working state to a previous point in time without losing his current changes. All of the answers revolved around, in various ways, something like</p>

<pre><code>git branch -m master crap_work
git branch -m previous_master master
</code></pre>

<p>How does this compare to <code>git stash</code>? I'm a bit confused trying to see what the different use case here when it <em>seems</em> like everything <code>git stash</code> does is already handled by branching…</p>

<hr>

<p>@<a href="https://stackoverflow.com/questions/39651/git-stash-vs-git-branch#39862">Jordi Bunster</a>: Thanks, that clears things up. I guess I'd kind of consider "stashing" to be like a lightweight, nameless, branch. So anything stash can do, branch can as well but with more words. Nice!</p>

[{'answer_id': 39862, 'author': 'Jordi Bunster', 'author_id': 4272, 'author_profile': 'https://Stackoverflow.com/users/4272', 'pm_score': 8, 'selected': True, 'text': '<p>\'stash\' takes the uncommitted, "<em>dirty</em>" stuff on your working copy, and stashes it away, leaving you with a clean working copy.</p>\n\n<p>It doesn\'t really branch at all. You can then apply the stash on top of any other branch. Or, as of Git 1.6, you can do:</p>\n\n<pre><code>git stash branch &lt;branchname&gt; [&lt;stash&gt;]\n</code></pre>\n\n<p>to apply the stash on top of a new branch, all in one command.</p>\n\n<p>So, stash works great if you have not committed to the "<em>wrong</em>" branch yet.</p>\n\n<p>If you\'ve already committed, then the workflow you describe in your question is a better alternative. And by the way, you\'re right: Git is very flexible, and with that flexibility comes overlapping functionality.</p>\n'}, {'answer_id': 134840, 'author': 'webmat', 'author_id': 6349, 'author_profile': 'https://Stackoverflow.com/users/6349', 'pm_score': 4, 'selected': False, 'text': "<p>I'm always wary of git stash. If you stash a few times, things tend to get messy. git stash list will display a numbered list of stashes you created, with messages if you provided them... But the problem lies in the fact that you can't clean up stashes except with a brutal git stash clear (which removes them all). So unless you're always consistently giving super-descriptive messages for your stashes (kinda goes against stash's philosophy), you end up with an incomprehensible bunch of stashes.</p>\n<p>The only way I know of to figure out which one's which is to use gitk --all and spot the stashes. At least this lets you see what commit the stash was created on, as well as the diff of everything included in that stash.</p>\n<p>Note that I'm using git 1.5.4.3, and I think 1.6 adds git stash pop, which I guess would apply the selected stash <em>and</em> remove it from the list. Which seems a lot cleaner.</p>\n<p>For now, I always try to branch unless I'm absolutely positive I'm gonna get back to that stash in the same day, even within the hour.</p>\n"}, {'answer_id': 955657, 'author': 'Ariejan', 'author_id': 117975, 'author_profile': 'https://Stackoverflow.com/users/117975', 'pm_score': 6, 'selected': False, 'text': '<p>When you restore your stash, your changes are reapplied and you continue working on your code.</p>\n\n<p>To stash your current changes</p>\n\n<pre><code>$ git stash save \nSaved "WIP on master: e71813e..."\n</code></pre>\n\n<p>You can also have more than one stash. The stash works like a stack. Every time you save a new stash, it\'s put on top of the stack.</p>\n\n<pre><code>$ git stash list\nstash@{0}: WIP on master: e71813e..."\n</code></pre>\n\n<p>Note the <code>stash@{0}</code> part? That\'s your stash ID. You\'ll need it to restore it later on. Let\'s do that right now. The stash ID changes with every stash you make. stash@{0} refers to the last stash you made.</p>\n\n<p>To apply a stash</p>\n\n<pre><code>$ git stash apply stash@{0}\n</code></pre>\n\n<p>You may notice the stash is still there after you have applied it. You can drop it if you don\'t need it any more.</p>\n\n<pre><code>$ git stash drop stash@{0}\n</code></pre>\n\n<p>Or, because the stash acts like a stack, you can pop off the last stash you saved:</p>\n\n<pre><code>$ git stash pop\n</code></pre>\n\n<p>If you want to wipe all your stashes away, run the \'clear\' command:</p>\n\n<pre><code>$ git stash clear\n</code></pre>\n\n<p>It may very well be that you don\'t use stashes that often. If you just want to quickly stash your changes to restore them later, you can leave out the stash ID.</p>\n\n<pre><code>$ git stash\n...\n$ git stash pop\n</code></pre>\n\n<p>Feel free to experiment with the stash before using it on some really important work.</p>\n\n<p><a href="http://ariejan.net/2008/04/23/git-using-the-stash/" rel="noreferrer">I also have a more in-depth version of this posted on my blog</a>.</p>\n'}, {'answer_id': 31498125, 'author': 'Dan Aloni', 'author_id': 382213, 'author_profile': 'https://Stackoverflow.com/users/382213', 'pm_score': 2, 'selected': False, 'text': '<p>If you look for a workflow that may be more fitting than git stash, you may want to look at <a href="https://github.com/da-x/git-bottle" rel="nofollow" title="git-bottle">git-bottle</a>. It\'s a utility for the purpose of saving and restoring the various git working states as normal git commits, effectively snapshotting the current and pertinent state of your working tree and <strong>all</strong> various file states shown under git status.</p>\n\n<p>Key differences from <code>git stash</code>:</p>\n\n<ul>\n<li><code>git stash</code> saves the dirty git state narrowly (modified files, and added files in the index), whereas <code>git-bottle</code> is designed to save <em>everything</em> that is different from <code>HEAD</code>, and it differentiates in a preserving way between modified, modified and not added, not added, unmerged paths, and the complete rebase/merge states (only paths under <code>.gitignore</code> are not saved).</li>\n<li><code>git stash</code> saves to stash objects that you need to keep track separately. If I stashed something 2 weeks ago I might not remember it, whereas <code>git-bottle</code> saves as <strong>tentative commits to the current branch</strong>. The reverse action is <code>git-unbottle</code> which is the equivalent of the <code>git stash</code> pop. It is possible to push and share these commits among repositories. This can be useful for remote builds, where you have another repository in a remote server just for building, or for collaborating with other people on conflict resolution.</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/39651', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4161/']