What is the intended use-case for git stash?

Alexander picture Alexander · Dec 12, 2013 · Viewed 103.4k times · Source

If I work on branch A and suddenly need to work on branch B before being ready with a commit on branch A, I stash my changes on A, checkout B, do my work there, then checkout A and apply the stash.

If I work on A and I want to stop working for the day, should I stash my work and then apply it the next day (when I resume my work), or should I just leave things as they are—uncommitted modified files in the working directory? I don't see why I would need to use stash in this case, except if there is some security benefit.

Also, another scenario: I work both at work and at home. If I am not ready with a commit when I want to go home, can I stash my work, push it to GitHub and then pull that stash at home?

Answer

Mureinik picture Mureinik · Dec 12, 2013

Stash is just a convenience method. Since branches are so cheap and easy to manage in git, I personally almost always prefer creating a new temporary branch than stashing, but it's a matter of taste mostly.

The one place I do like stashing is if I discover I forgot something in my last commit and have already started working on the next one in the same branch:

# Assume the latest commit was already done
# start working on the next patch, and discovered I was missing something

# stash away the current mess I made
git stash save

# some changes in the working dir

# and now add them to the last commit:
git add -u
git commit --amend

# back to work!
git stash pop