On Tue, 23 Oct 2012 10:56:21 -0700 (PDT)
Kayatta Schmidt <joshuelpatterso...@gmail.com> wrote:

> This is where I get confused.  My intuition tells me there is no
> reason to stash pop until I've checked out *branchA *again, but there
> seems to be very little clarification on how the pop affects the
> branches I am currently working on...  *So *my question is - when I
> pop and/or apply the stashed changes - since I'm still in *branchB
> *will it mess up any of the code in *branchB*?  If yes - why?  If not
> - would I need to do another git add/commit before it does?

Your intuition is good, as it suggested you the right thing (mostly).

To better grasp the concept of the stash, it's useful to think of it
as a "patch file" saved somewhere.  I mean, when you do `git stash
save`, you can think of it doing `git diff >some.patch`, moving
that "some.patch" somewhere and then doing `git reset --hard HEAD`
to discard the changes in your work tree.
When you later `git stash apply` or `git stash pop`, you can think of
applying that patch file to your work tree as if you did something
like `patch -p0 <some.patch`.

In fact, the stash has more complicated mechanics as it saves your
local changes using two commits: one is created out of the state
of the index, and another one -- out of the state of your
modified changes.  You can see this running `git log` on a stash entry,
like `git log stash@{0}`.

Git does not remember which branch you were on when recording a stash;
the chain of two commits representing the stashed state just references
the commit which has been pointed to by HEAD at the time you stashed
your work.  It means Git won't attempt to switch you to any branch when
you tell it to apply a stashed state.

Now back to applying stashes.
The crucial thing to understand about it is that applying a stash
only modifies your work tree and never records a commit, hence applying
a stash cannot mess up any code on any branch because no commit is ever
recorded on any branch.

On the other hand, while techncally you can try to apply a stashed
state to any checkout, it's logical that chances of successfull
application heavily depend on how different that checkout is compared
to the state the stash has been recorded against.

So if we consider your example, you can try to apply your stash to any
branch, but you have to check it out yourself before attempting to do
that.  Keep in mind that stashing is about saving local modifications
to the work tree, not about saving an imaginary "uncommitted state of
a branch I was on" as local modifications are not on any branch.

As usually, it's recommended to read [1].  One point of interest might
be the ability to create a branch out of the stashed state.

1. http://git-scm.com/book/en/Git-Tools-Stashing

You received this message because you are subscribed to the Google Groups "Git 
for human beings" group.
To post to this group, send email to git-users@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to