988

I had some uncommitted changes in my development branch and I stashed them using git stash, but there were some changes which were very important among those stashed ones. Is there any way to get back those changes?

Also, I have made some changes on top of the stashed code files since.

Is there any chance I can retrieve the stashed changes to a new branch if possible?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Aswathy P Krishnan
  • 11,728
  • 7
  • 27
  • 45
  • 12
    have you tried using 'stash pop'? – robert Sep 25 '13 at 10:58
  • 1
    No. Actually I am new to git. As I am not fully aware of all the commands i didn't try anything else! I dont wanna lose those changes. – Aswathy P Krishnan Sep 25 '13 at 11:00
  • 56
    If you dont want lose the stashed changes, try using 'git stash apply'. This will apply the stashed changes to you current branch, while still retaining the stash. If everything is ok, after applying the stash, you can drop the stash, using 'git stash drop' – robert Sep 25 '13 at 11:03

8 Answers8

1672

The easy answer to the easy question is git stash apply

Just check out the branch you want your changes on, and then git stash apply. Then use git diff to see the result.

After you're all done with your changes—the apply looks good and you're sure you don't need the stash any more—then use git stash drop to get rid of it.

I always suggest using git stash apply rather than git stash pop. The difference is that apply leaves the stash around for easy re-try of the apply, or for looking at, etc. If pop is able to extract the stash, it will immediately also drop it, and if you subsequently realize that you wanted to extract it somewhere else (in a different branch), or with --index, or some such, that's not so easy. If you apply, you get to choose when to drop.

It's all pretty minor one way or the other though, and for a newbie to Git, it should be about the same. (And you can skip all the rest of this!)


What if you're doing more-advanced or more-complicated stuff?

There are at least three or four different "ways to use git stash", as it were. The above is for "way 1", the "easy way":

  1. You started with a clean branch, were working on some changes, and then realized you were doing them in the wrong branch. You just want to take the changes you have now and "move" them to another branch.

This is the easy case, described above. Run git stash save (or plain git stash, same thing). Check out the other branch and use git stash apply. This gets Git to merge in your earlier changes, using Git's rather powerful merge mechanism. Inspect the results carefully (with git diff) to see if you like them, and if you do, use git stash drop to drop the stash. You're done!

  1. You started some changes and stashed them. Then you switched to another branch and started more changes, forgetting that you had the stashed ones.

Now you want to keep, or even move, these changes, and apply your stash too.

You can in fact git stash save again, as git stash makes a "stack" of changes. If you do that, you have two stashes, one just called stash—but you can also write stash@{0}—and one spelled stash@{1}. Use git stash list (at any time) to see them all. The newest is always the lowest-numbered. When you git stash drop, it drops the newest, and the one that was stash@{1} moves to the top of the stack. If you had, even more, the one that was stash@{2} becomes stash@{1}, and so on.

You can apply and then drop a specific stash, too: git stash apply stash@{2}, and so on. Dropping a specific stash renumbers only the higher-numbered ones. Again, the one without a number is also stash@{0}.

If you pile up a lot of stashes, it can get fairly messy (was the stash I wanted stash@{7} or was it stash@{4}? Wait, I just pushed another, now they're 8 and 5?). I personally prefer to transfer these changes to a new branch, because branches have names, and cleanup-attempt-in-December means a lot more to me than stash@{12}. (The git stash command takes an optional save-message, and those can help, but somehow, all my stashes just wind up named WIP on branch.)

  1. (Extra-advanced) You've used git stash save -p, or carefully git add-ed and/or git rm-ed specific bits of your code before running git stash save. You had one version in the stashed index/staging area and another (different) version in the working tree. You want to preserve all this. So now you use git stash apply --index, and that sometimes fails with:

    Conflicts in index.  Try without --index.
    
  2. You're using git stash save --keep-index in order to test "what will be committed". This one is beyond the scope of this answer; see this other StackOverflow answer instead.

For complicated cases, I recommend starting in a "clean" working tree first, by committing any changes you have now (on a new branch if you like). That way the "somewhere" that you are applying them, has nothing else in it, and you'll just be trying the stashed changes:

git status               # see if there's anything you need to commit
                         # uh oh, there is - let's put it on a new temp branch
git checkout -b temp     # create new temp branch to save stuff
git add ...              # add (and/or remove) stuff as needed
git commit               # save first set of changes

Now you're on a "clean" starting point. Or maybe it goes more like this:

git status               # see if there's anything you need to commit
                         # status says "nothing to commit"
git checkout -b temp     # optional: create a new branch for "apply"
git stash apply          # apply stashed changes; see below about --index

The main thing to remember is that the "stash" is a commit, it's just a slightly "funny/weird" commit that's not "on a branch". The apply operation looks at what the commit changed and tries to repeat it wherever you are now. The stash will still be there (apply keeps it around), so you can look at it more, or decide this was the wrong place to apply it and try again differently, or whatever.


Any time you have a stash, you can use git stash show -p to see a simplified version of what's in the stash. (This simplified version looks only at the "final work tree" changes, not the saved index changes that --index restores separately.) The command git stash apply, without --index, just tries to make those same changes in your working tree now.

This is true even if you already have some changes. The apply command is happy to apply a stash to a modified working tree (or at least, to try to apply it). You can, for instance, do this:

git stash apply stash      # apply top of stash stack
git stash apply stash@{1}  # and mix in next stash stack entry too

You can choose the "apply" order here, picking out particular stashes to apply in a particular sequence. Note, however, that each time you're basically doing a "git merge", and as the merge documentation warns:

Running git merge with non-trivial uncommitted changes is discouraged: while possible, it may leave you in a state that is hard to back out of in the case of a conflict.

If you start with a clean tree and are just doing several git apply operations, it's easy to back out: use git reset --hard to get back to the clean state, and change your apply operations. (That's why I recommend starting in a clean working tree first, for these complicated cases.)


What about the very worst possible case?

Let's say you're doing Lots Of Advanced Git Stuff, and you've made a stash, and want to git stash apply --index, but it's no longer possible to apply the saved stash with --index because the branch has diverged too much since the time you saved it.

This is what git stash branch is for.

If you:

  1. check out the exact commit you were on when you did the original stash, then
  2. create a new branch, and finally
  3. git stash apply --index

the attempt to re-create the changes definitely will work. This is what git stash branch newbranch does. (And it then drops the stash since it was successfully applied.)


Some final words about --index (what the heck is it?)

What the --index does is simple to explain, but a bit complicated internally:

  • When you have changes, you have to git add (or "stage") them before commit-ing.
  • Thus, when you ran git stash, you might have edited both files foo and zorg, but only staged one of those.
  • So when you ask to get the stash back, it might be nice if it git adds the added things and does not git add the non-added things. That is, if you are add-ed foo but not zorg back before you did the stash, it might be nice to have that exact same setup. What was staged, should again be staged; what was modified but not staged, should again be modified but not staged.

The --index flag to apply tries to set things up this way. If your working tree is clean, this usually just works. If your working tree already has stuff add-ed, though, you can see how there might be some problems here. If you leave out --index, the apply operation does not attempt to preserve the whole staged/unstaged setup. Instead, it just invokes Git's merge machinery, using the working tree commit in the "stash bag". If you don't care about preserving staged/unstaged, leaving out --index makes it a lot easier for git stash apply to do its thing.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Sorry! I made some changes after stashing the changes to try is I could make it without those lost codes. So will I be able to get the last stashed changes back? – Aswathy P Krishnan Sep 25 '13 at 11:26
  • 2
    I don't understand your comment. Do you mean: you ran `git stash pop`? Or do you mean: you edited some files, but have not run `git stash` again yet? Or do you mean something else entirely? – torek Sep 25 '13 at 11:35
  • In such a case will `git stash apply` work? Please help me with a solution..! – Aswathy P Krishnan Sep 25 '13 at 11:59
  • 2
    Yes. Note, in my (long) edit I recommend committing what you have now before `apply`-ing a stash. You don't *have* to do this, but it makes things a lot simpler for you to look at. You can use `rebase -i` to squash together multiple commits, or cherry-pick particular changes, or whatever, later. – torek Sep 25 '13 at 12:28
  • Thanks A lot @torek!!! I am going through your anwser and going as per the instructions. I created a branch temp, did `git add.`. After that I committed those changes. I am a bit confused about `--index` as you mentioned. So can I just run `git stash apply index`? I am in `temp` branch now. – Aswathy P Krishnan Sep 25 '13 at 12:41
  • 1
    Yes: `git stash apply --index` (remember the two dashes). If you leave out `--index`, no big deal; the only point of `--index` is to keep the staged/unstaged setup. (You probably didn't have any special setup in the first place.) Then `git status` etc, and add/commit as desired etc. When (and only when) you're all done with the stash, use `git stash drop` to discard it. – torek Sep 25 '13 at 12:45
  • One doubt before executing `git stash apply --index`. I just did a commit for some guessed code. But will that change my stashed code when I get that back? I might seem a bit foolish, but The code I stashed is more important, that is why. – Aswathy P Krishnan Sep 25 '13 at 12:50
  • 2
    As long as you keep (do not `drop` or `pop`) the stash, you always have the original stashed code safe on a commit, because a stash *is* a commit! If you want to get it back exactly, but on a branch, use `git stash branch` (see that section above, or the [Pro Git book](http://git-scm.com/book/ch6-3.html) in [Shunya's answer](http://stackoverflow.com/a/19005203/1256452)). You can then `git checkout` that branch, or `git cherry-pick` the commit off that branch, etc. – torek Sep 25 '13 at 12:55
  • I got `Conflict in index. try without --index`. I had replaced an old JAR with an updated one (which I lost during the stash). – Aswathy P Krishnan Sep 25 '13 at 13:01
  • Will `git stash apply` instead of `git stash apply --index` work in the same manner? – Aswathy P Krishnan Sep 25 '13 at 13:16
  • 1
    I really wish they had chosen something more intuitive like "git stash restore" instead of "git stash apply". When I stash something, I expect to be able to "restore" it later. – Chuck Wolber May 10 '18 at 18:52
  • 2
    @ChuckWolber: Git's naming conventions leave a lot to be desired (how many different meanings can we assign to the words "remote", "tracking", and "branch"?!). It's worth noting that you can apply a stash to something unrelated to the original stash, though. – torek May 10 '18 at 18:54
112
git stash pop

will get everything back in place

as suggested in the comments, you can use git stash branch newbranch to apply the stash to a new branch, which is the same as running:

git checkout -b newbranch
git stash pop
Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
Stefano Falasca
  • 8,837
  • 2
  • 18
  • 24
  • Thanks for the help. Can I get those changes into a new branch? Right now i am on develop branch – Aswathy P Krishnan Sep 25 '13 at 11:01
  • 3
    git stash branch newbranch, will create a new branch with the stashed changes. – robert Sep 25 '13 at 11:04
  • 3
    @robert: `git stash branch newbranch` will indeed do that; but be aware that it creates the new branch with its parent set to the commit that was the `HEAD` at the time the `stash` was done. In other words, it's for when you come back after some long hack session or whatever, stare at the mess, and decide "I should have put that on a branch, rather than stashing" :-) – torek Sep 25 '13 at 11:06
  • I have edited my question. I would like to get those changes to a new branch if possible. – Aswathy P Krishnan Sep 25 '13 at 11:19
  • 1
    @sachinruk sometimes? always, people don't come to StackOverflow for documentation, that's not the purpose and by that, I am not criticising well-written answers neither defending 1 liners. – Mel Macaluso Nov 26 '19 at 10:36
42

To check your stash content :-

git stash list

apply a particular stash no from stash list:-

git stash apply stash@{2}

or for applying just the first stash:-

git stash pop

Note: git stash pop will remove the stash from your stash list whereas git stash apply wont. So use them accordingly.

37

To make this simple, you have two options to reapply your stash:

  1. git stash pop - Restore back to the saved state, but it deletes the stash from the temporary storage.
  2. git stash apply - Restore back to the saved state and leaves the stash list for possible later reuse.

You can read in more detail about git stashes in this article.

Nesha Zoric
  • 6,218
  • 42
  • 34
14

To get back the stashed changes... you can use a single command

git stash pop

To see the list of stashed changes

git stash list

To clear stash changes

git stash clear
Meet Bhalodiya
  • 630
  • 7
  • 25
10

On mac this worked for me:

git stash list(see all your stashs)

git stash list

git stash apply (just the number that you want from your stash list)

like this:

git stash apply 1
Zack117
  • 985
  • 1
  • 13
  • 28
5

You can stash the uncommitted changes using:

git stash

Then checkout to a new branch using:

git checkout -b

Then apply the stashed commits:

git stash apply
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
0

as many people say doing git stash apply stash@{1} will get the changes on current branch, i don't know why doesn't that work for me.

for me doing git stash apply stashNumber works all the times.

for example- if i want to retrieve stash number 1 so this is how will do it- git stash apply 1

PS: you can use pop in place of apply.. the only difference is that apply won't remove the stash but doing pop will remove the stashnumber you are popping out.

navinrangar
  • 904
  • 10
  • 20