5

I have an issue in git stash.

Say for example I have 3 files say a.txt,b.txt & c.txt in the repo and the directory is clean.

Now I'm modifying 2 files among them : a.txt and b.txt.

Now I havent completed my changes in thw two files so I am stashing them by the foll command:

$ git stash save "First Stash" 

No if I do a $ git stash list, I get

stash@{0}: On master: First Stash 

No if I modify the third text file c.txt and stash that as well as shown:

$ git stash save "Second Stash" 

No finally if I do a $git stash list I'm getting the foll result,

stash@{0}: On master: Second stash stash@{1}: On master: First Stash 

The stash number and the messages are mixed up here.What's going on here? Now if i pop the stash@{0} I get the first stash contents but the message reversed here which is displaying as Second stash but should have been First Stash.

This is my work flow

admin:stud:/demo/stash_demo> ls a.txt b.txt admin:stud:/demo/stash_demo> echo Hello World >> a.txt admin:stud:/demo/stash_demo> git stash save "First" Saved working directory and index state On master: First HEAD is now at cff03c6 Initail Commit admin:stud:/demo/stash_demo> echo Hello World >> b.txt admin:stud:/demo/stash_demo> git stash save "Second" Saved working directory and index state On master: Second HEAD is now at cff03c6 Initail Commit 

These are my available stashes:

admin:stud:/demo/stash_demo> git stash list stash@{0}: On master: Second stash@{1}: On master: First 

Now I''ll be trying to apply the stash@{1} which is the first stash and should apply the file a.txt

admin:stud:/demo/stash_demo> git stash apply `stash@{1}` # On branch master # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: b.txt # no changes added to commit (use "git add" and/or "git commit -a") 

As seen above I get the most recent changes applied.

And if i try without the ticks `` then it gives me the foll error.

admin:stud:/demo/stash_demo> git stash apply stash@{1} fatal: ambiguous argument 'stash@1': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' 
6
  • Did you use ´stash@{1}´, including the backticks? I recall git stash apply ignoring unknown stash names silently and just applying stash@{0} instead. Commented Jan 29, 2013 at 14:27
  • Yes I did include the backticks. Commented Jan 29, 2013 at 16:33
  • So, try it without them. ;) Commented Jan 30, 2013 at 7:28
  • I tried without the ticks it gives me an error saying its an ambiguous argument.I'll update the question the entire output its giving. Commented Jan 30, 2013 at 12:11
  • I noticed just now that when I apply the the stash@{1} with the backticks I get the message stash@1: Command not found and the most recent stash is applied.I think the syntax which I'm using is wrong.But if I try without he backticks nothing happens. Commented Jan 30, 2013 at 12:22

2 Answers 2

5

Git's stash is (by default) a LIFO storage.

By using git stash apply or git stash pop you will always get the last stashed patch (which can be also referenced using stash@{0}, 0 is the index inside the stash). Stashing a new patch will place it on top of other stashed patches, thus increasing their indices by one (stash@{0} will become stash@{1}).

That said, if you want to apply or pop a patch other than the last one stashed, you need pass its ref to the command:

$ git stash apply stash@{1} 
Sign up to request clarification or add additional context in comments.

5 Comments

Even if I'm applying stash@{1} I'm getting the most recent changes applied to the repo.
Did you verify that stash@{1} is what you want? For example with git stash list or git stash show stash@{1}.
When I try git stash show stash@{1} and git stash show stash@{0} I'm getting the same output. And whatever I apply or pop I get the most recent changes.And even when I want stash@{1} i get the most recent changes.
That means that both stash@{0} and stash@{1} have the same content, so you might have stashed twice by mistake, e.g. using git stash; git apply; git stash. If you're sure the contents are the same, you can safely drop one of them: git stash drop stash@{1}.
No they are not same.First time I modified a.txt and stashed it.Later I modified b.txt and stashed it.Now when I list all the stashed contents I get the result that i have 2 stashes available.Now regardless of what I apply or pop I am getting b.txt applied then when i apply the other stash i get a.txt.Give me 2 mins and I will pate the entire log as edit in the question.
1

they are not mixed up!

Stashing stores the "commits" (not the correct word here, I know) in a Stack:

http://en.wikipedia.org/wiki/Stack_(abstract_data_type)

so the operation pop always gets you the last thing you put in there.

This behaviour is by design

edit: if you need to flip something around, then pop each item in the stack(stash) individually - do a real commit - and interactive rebase or whatever ..

edit/edit: do a

git stash pop 

then commit your changes (regardless of whether this commit is usefull or not)

then do a git stash pop again, commit again

now you can reorder or delete single commits - take a look here: http://gitready.com/advanced/2009/03/20/reorder-commits-with-rebase.html or search for interactive rebasing (again .. the next time do a branch this concept is much more powerfull then stashing)

5 Comments

Is there any way to get the first thing i stashed?
like i said (in the edit) pop everything seperately and do commits (maybe in a different branch - to be clean) then you will eventually end up with the first thing you stashed
btw: i realy prefer doing a quick branch over stashing because you have so much more control over everything..
The git stash apply stash@{n} has no meaning right, since it always pops/applies the most recent stash.I have added my workflow as an edit in the question
i tried to describe the solution a little better in the "edit/edit" part

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.