stash@{1} is ambiguous?
Asked Answered
I

5

141

I'm trying to get info about my stash, but git is telling me that stash@{0} and stash@{1} are ambiguous. git stash list works fine, and .git/logs/refs/stash seems to have the appropriate content (not that I'm an expert on git internals).

% git stash list
stash@{0}: On master: two
stash@{1}: On master: one
% git stash show stash@{1}
fatal: ambiguous argument 'stash@1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

Just plain git stash show works fine. So why are the names that git stash list gives me considered ambiguous?

Idiographic answered 24/6, 2011 at 14:2 Comment(2)
In my case, I'd somehow ended up with a branch named stash, which was causing the error.Condor
And in my case I had a remote named stash, causing the error!Pomerleau
E
269

Your shell is eating your curly brackets, so while you say stash@{1}, git sees stash@1, and that makes no sense to it. Quote the argument (use git stash apply "stash@{1}" or git stash apply stash@"{1}"; quoting either way will work) or reconfigure your shell to only expand curly brackets when there is a comma between them (zsh can be configured either way; bash only expands curly brackets with a comma or range between them while other shells may behave differently).

Enlargement answered 24/6, 2011 at 14:6 Comment(12)
which shell? In bash, this is not true (by default)Pifer
@sehe: The one Uncommon currently uses. The error message quoted indicates it quite clearly (no, it seems it can't be bash).Enlargement
Thanks! I forgot I still had tcsh as the default on this computer.Idiographic
On Windows, PowerShell will eat brackets too. You can escape them with a backtick (git stash drop stash@`{1`})Bleeding
@XavierPoinas, didn't help, just have ambiguous argument 'stash@`0`'Batholith
Thanks this happens with Fish, was not sure what was happening.Harhay
@Batholith use either stash@`{0`} (backticks) or 'stash@{0}' (single quotes) for PowerShell.Alina
For windows cmd shell, use "stash@{0}" (double quotes)Sahaptin
Oh my, over a year later and I came back to this again because I was having an issue. Just shows how rarely I use this. Then to see I left a comment above that sure enough, says it happens with Fish. Too funny.Harhay
See this answer on how to actually make zsh stop expanding invalid brace expressions.Winniewinnifred
FYI, the error I get from PowerShell for this is "unknown option: -encodedCommand". And surrounding it with double quotes works.Actinomycin
@XavierPoinas, your code example is very treacherous. Why don’t you replace 'drop' into 'apply'? ("Lifeline" just for inattentive person, as me - https://mcmap.net/q/11480/-how-do-i-recover-a-dropped-stash-in-git)Pinky
E
25

I had the same thing happen to me. The easiest way I found to fix it was:

$ git stash apply stash@"{2}"

I'm using a windows git shell.

Endora answered 14/8, 2015 at 16:0 Comment(3)
Also git stash apply "stash@{2}"Winniewinnifred
Works for powershellWinniewinnifred
same git stash apply "stash@{2}" worked for me, i was getting "unknown option: -encodedCommand" message when trying to git stash my 3rd stash, i was using visual studio code which have powershell integration as it's terminal.Haworth
L
2

Simply put stash id between simple quotes:

git stash apply 'stash@{1}'
Lenient answered 8/3, 2017 at 14:40 Comment(3)
How does this answer the question? There is no #3 index in stash list. If this is a serious answer please explain why this command will solve the problemApparel
I edited my answer for clarity: the main idea was to simply puts stash id between simple quotes. It applies to 'apply' or 'drop' subcommands. The stash id number does not actually matterLenient
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.Wooded
B
1

If you have this error while working in Emacs with Magit on Windows (like me)
I hope this quick solution will help you:

(if (eq system-type 'windows-nt)
    (defadvice magit-run-git (before magit-run-git-win-curly-braces (&rest args) activate)
      "Escape {} on Windows"
      (setcar (nthcdr 2 args) 
              (replace-regexp-in-string "{\\([0-9]+\\)}" "\\\\{\\1\\\\}" (elt args 2)))
    )
  )

This will quote {} in a 3rd parameter in ("stash", "cmd", "stash@{0}") which is run by magit-run-git

Batholith answered 3/1, 2013 at 10:28 Comment(0)
P
1

For zsh users:

$ git stash apply stash@'{'1'}'
Pedersen answered 25/8, 2015 at 2:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.