git stash apply version

GitGit Stash

Git Problem Overview


I have 2 branches: master | design

Working in design I did a stash and switched to master, made some adjustments. Switched back to design and did a stash apply only to lose all my changes in the design branch.

I am hoping all my work is within a stash as I have not cleared or removed these.

If I do a stash list I get 4 results:

stash@{0}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{1}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{2}: WIP on design: eb65635... Email Adjust
stash@{3}: WIP on design: eb65635... Email Adjust

If I try git stash apply f2c0c72 I am getting an error:

fatal: Needed a single revision
f2c0c72: no valid stashed state found

How can I apply a specific stash?

Git Solutions


Solution 1 - Git

The keys into the stash are actually the stash@{n} items on the left. So try:

git stash apply stash@{0}

(note that in some shells you need to quote "stash@{0}", like zsh, fish and powershell).

Since version 2.11, it's pretty easy, you can use the N stack number instead of using stash@{n}. So now instead of using:

git stash apply "stash@{n}"

You can type:

git stash apply n

To get list of stashes:

git stash list

In fact stash@{0} is a revision in git that you can switch to... but git stash apply ... should figure out how to DTRT to apply it to your current location.

Solution 2 - Git

To apply a stash and remove it from the stash list, run:

git stash pop stash@{n}

To apply a stash and keep it in the stash cache, run:

git stash apply stash@{n}

Solution 3 - Git

Since version 2.11, it's pretty easy, you can use the N stack number instead of saying "stash@{n}". So now instead of using:

git stash apply "stash@{n}"

You can type:

git stash apply n

For example, in your list:

stash@{0}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{1}: WIP on design: f2c0c72... Adjust Password Recover Email
stash@{2}: WIP on design: eb65635... Email Adjust
stash@{3}: WIP on design: eb65635... Email Adjust

If you want to apply stash@{1} you could type:

git stash apply 1

Otherwise, you can use it even if you have some changes in your directory since 1.7.5.1, but you must be sure the stash won't overwrite your working directory changes if it does you'll get an error:

error: Your local changes to the following files would be overwritten by merge:
        file
Please commit your changes or stash them before you merge.

In versions prior to 1.7.5.1, it refused to work if there was a change in the working directory.


Git release notes:

> The user always has to say "stash@{$N}" when naming a single element > in the default location of the stash, i.e. reflogs in refs/stash. The > "git stash" command learned to accept "git stash apply 4" as a > short-hand for "git stash apply stash@{4}"

> git stash apply" used to refuse to work if there was any change in the > working tree, even when the change did not overlap with the change the > stash recorded

Solution 4 - Git

If one is on a Windows machine and in PowerShell, one needs to quote the argument such as:

git stash apply "stash@{0}"

...or to apply the changes and remove from the stash:

git stash pop "stash@{0}"

Otherwise without the quotes you might get this error:

> fatal: ambiguous argument 'stash@': unknown revision or path not in > the working tree.

Solution 5 - Git

git stash list 

List will show all stashed items eg:stash@{0}:,stash@{1}:,..,stash@{n}:

Then select the number n which denotes stash@{n}:

git stash apply n 

for example:

git stash apply 1 

will apply that particular stashed changes to the current branch

Solution 6 - Git

To view your recent work and what branch it happened on run

git stash list

then select the stash to apply and use only number:

git stash apply n

Where n (in the above sample) is that number corresponding to the Work In Progress.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeeView Question on Stackoverflow
Solution 1 - GitaraqnidView Answer on Stackoverflow
Solution 2 - GitdwlzView Answer on Stackoverflow
Solution 3 - GitPauView Answer on Stackoverflow
Solution 4 - GitjterryView Answer on Stackoverflow
Solution 5 - GitHarshavardhan reddy.View Answer on Stackoverflow
Solution 6 - GitpanthariView Answer on Stackoverflow