Search code examples
gitgit-stash

Git stash issue


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>...]'

Solution

  • 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}