Choosing Your Stash

Learn how to deal with multiple stashes that are on the stack.

Now, you have two changes in your stash’s stack.

Applying two stashes#

Type this sequence out. It will stash two similar-looking changes.

1   mkdir lgthw_git_stash_2
2   cd lgthw_git_stash_2
3   git init
4   echo 'Some content' > file1
5   git add file1
6   git commit -am initial
7   echo 'First changes I am not sure about' >> file1
8   git stash
9   echo 'Second change I am also not sure about' >> file1
10  git stash
11  git stash list
Terminal 1
Terminal

Click to Connect...

You can see that now you have two changes in your stash’s stack. But which is which? Is stash@{0} the first change, or the second one?

Getting individual stash information#

Some minimal information is available if you type git stash show <ID>:

12  git stash show stash@{0}
13  git stash show stash@{1}
Terminal 1
Terminal

Click to Connect...

But this is not sufficient enough for you to tell what is going on. It just tells you that one line was inserted in each change.

git stash show --patch <ID> gives you the information in a different format also. Run this command in the terminal provided above:

14  git stash show --patch stash@{0}
15  git stash show --patch stash@{1}

From this, you can infer that stash pushes to a stack at number zero, and then pops from zero if you use git stash pop.

Applying a specific stash#

Run the following command if you want to apply the first change only:

16  git stash apply stash@{1}
17  git diff
Terminal 1
Terminal

Click to Connect...

Be aware of a little “gotcha” here; if you apply a git stash, then it remains in the list. git stash drop will then remove the stash item for you.

18  git stash list

Go ahead and remove the stash items yourself with the help of git stash drop.

Dealing With a Scenario
Challenge: Git Stash
Mark as Completed
Report an Issue