Git Question

What are the real benefits of stash command?

I came across a

stash
command in git and I really don't understand benefits of such command.

Suppose I created some temporary changes in
foo_branch
, and I want to checkout on
bar_branch
. My usual practise is to create commit with
temp commit
message in
foo_branch
, and then perform checkout on
bar_branch
. Then if I want to come back to
foo_branch
, I perform checkout and discard last commit (preserving changes) of
foo_branch
, then I continue to work.

In my opinion,
stash
command is just another way to do such thing.

What are the real benefits of
stash
command? Maybe is it just shortcut for such case?

Answer

The stash command can save the current state of the index, the working dir and of untracked files. It's just a lot quicker to run git stash than it is to decide what changes to add to the staging area and commit with a commit message.

Moreover, a stash can be applied in a different position than where it was initially created. You could do that by temporarily commiting on the current branch, switching to a different branch, cherry-picking your commit, and then removing it from where it was originally created.

It's just a quick way of getting a clean working directory if you need to do something else.