I'm new to git and not quite clear on how stashing works.
Let's say I'm working on branch master and try to git pull and receive the error that my local changes would be overwritten and need to be stashed or committed. If I haven't staged any of my changes and run git stash, then do a git pull and and update successfully, what happens when I git stash apply?
In general, If someone else modifies files and I run git pull, what happens when I run git stash apply? does it overwrite the files that were just updated, regardless of whether or not they were staged when I stashed them? Does it overwrite every file that I just updated with git pull, with the files that were stashed?
Quick "TL;DR" take-away version, so one can come back later and study more
git stashhangs a stash-bag—this is a peculiar form of a merge commit that is not on any branch—on the currentHEADcommit. A latergit stash apply, when you're at any commit—probably a different commit—then tries to restore the changes git computes by looking at both the hanging stash-bag and the commit it hangs from.When you're done with the changes, you should use
git stash dropto let go of the stash-bag from the commit it was "stashed on". (And,git stash popis just shorthand for "apply, then automatically drop". I recommend keeping the two steps separate, though, in case you don't like the result of "apply" and you want to try again later.)The long version
git stashis actually fairly complex.It's been said that "git makes much more sense once you understand X", for many different values of "X", which generalizes to "git makes much more sense once you understand git". :-)
In this case, to really understand
stash, you need to understand how commits, branches, the index/staging-area, git's reference name space, and merges all work, becausegit stashcreates a very peculiar merge commit that is referred-to by a name outside the usual name-spaces—a weird kind of merge that is not "on a branch" at all—andgit stash applyuses git's merge machinery to attempt to "re-apply" the changes saved when the peculiar merge commit was made, optionally preserving the distinction between staged and unstaged changes.Fortunately, you don't actually need to understand all of that to use
git stash.Here, you're working on some branch (
master) and you have some changes that aren't ready yet, so you don't want to commit them on the branch.1 Meanwhile someone else put something good—or at least, you hope it's good—into theorigin/masterover on the remote repo, so you want to pick those up.Let's say that you and they both started with commits that end in
- A - B - C, i.e.,Cis the final commit that you had in your repo when you started working on branchmaster. The new "something good" commits, we'll callDandE.In your case you're running
git pulland it fails with the "working directory not clean" problem. So, you rungit stash. This commits your stuff for you, in its special weird stash-y fashion, so that your working directory is now clean. Now you cangit pull.In terms of drawing of commits (a graph like you get with
gitkorgit log --graph), you now have something like this. The stash is the little bag-of-i-wdangling off the commit you were "on", in yourmasterbranch, when you rangit stash. (The reason for the namesiandwis that these are the "i"ndex / staging-area and "w"ork-tree parts of the stash.)This drawing is what you get if you started working on
masterand never did any commits. The most recent commit you had was thusC. After making the stash,git pullwas able to add commitsDandEto your local branchmaster. The stashed bag of work is still hanging offC.If you made a few commits of your own—we'll call them
Y, for your commit, andZjust to have two commits—the result of the "stash then pull" looks like this:This time, after
stashhung its stash-bag offZ, thepull—which is justfetchthenmerge—had to do a real merge, instead of just a "fast forward". So it makes commitM, the merge commit. Theorigin/masterlabel still refers to commitE, notM. You're now onmasterat commitM, which is a merge ofEandZ. You're "one ahead" oforigin/master.In either case, if you now run
git stash apply, the stash script (it's a shell script that uses a lot of low level git "plumbing" commands) effectively2 does this:This diffs
stash, which namesw—the "work tree" part of the stash—against the correct3 parent. In other words, it finds out "what you changed" between the proper parent commit (CorZ, as appropriate) and the stashed work-tree. It then applies the changes to the currently-checked-out version, which is eitherEorM, again depending on where you started.Incidentally,
git stash show -preally just runs that samegit diffcommand (with no> /tmp/patchpart of course). Without-p, it runs the diff with--stat. So if you want to see in detail whatgit stash applywill merge in, usegit stash show -p. (This won't show you whatgit stash applycan attempt to apply from the index part of the stash, though; this is a minor gripe I have with the stash script.)In any case, once the stash applies cleanly, you can use
git stash dropto remove the reference to the stash-bag, so that it can be garbage-collected. Until you drop it, it has a name (refs/stash, akastash@{0}) so it sticks around "forever" ... except for the fact that if you make a new stash, thestashscript "pushes" the current stash into the stash reflog (so that its name becomesstash@{1}) and makes the new stash use therefs/stashname. Most reflog entries stick around for 90 days (you can configure this to be different) and then expire. Stashes don't expire by default, but if you configure this otherwise, a "pushed" stash can get lost, so be careful about depending on "save forever" if you start configuring git to your liking.Note that
git stash drop"pops" the stash stack here, renumberingstash@{2}tostash@{1}and makingstash@{1}become plainstash. Usegit stash listto see the stash-stack.1It's not bad to go ahead and commit them anyway, and then do a later
git rebase -ito squash or fixup further second, third, fourth, ..., nth commits, and/or rewrite the temporary "checkpoint" commit. But that's independent of this.2It's a fair bit more complex because you can use
--indexto try to keep staged changes staged, but in fact, if you look in the script, you'll see the actual command sequencegit diff ... | git apply --index. It really does just apply a diff, in this case! Eventually it invokesgit merge-recursivedirectly, though, to merge in the work tree, allowing the same changes to have been brought in from elsewhere. A plaingit applywould fail if your patch does something the "good stuff" commitsDandEalso does.3This uses git's parent-naming magic syntax, with a little advance planning inside the
stashscript. Because the stash is this funky merge commit,whas two or even three parents, but the stash script sets it up so that the "first parent" is the original commit,CorZ, as appropriate. The "second parent"stash^2is the index state at the time of the commit, shown asiin the little hanging stash-bag, and the "third parent", if it exists, is unstaged-and-maybe-ignored files, fromgit stash save -uorgit stash save -a.Note that I assume, in this answer, that you have not carefully staged part of your work-tree and that you are not using
git stash apply --indexto restore the staged index. By not doing any of this, you render theicommit pretty much redundant, so that we need not worry about it during theapplystep. If you are usingapply --indexor equivalent, and have staged items, you can get into a lot more corner cases, where the stash won't apply cleanly.These same caveats apply, with yet more corner cases, to stashes saved with
-uor-a, that have that third commit.For these extra-hard cases,
git stashprovides a way to turn a stash into a full-fledged branch—but I'll leave all that to another answer.