140

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?

Uncommon
  • 3,323
  • 2
  • 18
  • 36

5 Answers5

267

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 comma or range between them, other shells may behave one or other way).

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • which shell? In bash, this is not true (by default) – sehe Jun 24 '11 at 14:19
  • 1
    @sehe: The one Uncommon currently uses. The error message quoted indicates it quite clearly (no, it seems it can't be bash). – Jan Hudec Jun 24 '11 at 14:35
  • Thanks! I forgot I still had tcsh as the default on this computer. – Uncommon Jun 25 '11 at 02:11
  • 86
    On Windows, PowerShell will eat brackets too. You can escape them with a backtick ``(git stash drop stash@`{1`})`` – Xavier Poinas May 15 '12 at 06:52
  • @XavierPoinas, didn't help, just have `ambiguous argument 'stash@\`0\`'` – Sergey Jan 03 '13 at 08:15
  • 8
    Thanks this happens with Fish, was not sure what was happening. – Elijah Lynn Sep 05 '14 at 18:50
  • 4
    @Sergey use either stash@\`{0\`} (backticks) or 'stash@{0}' (single quotes) for PowerShell. – Hans Dec 28 '14 at 23:56
  • For windows cmd shell, use "stash@{0}" (double quotes) – Christian Long Feb 24 '15 at 04:24
  • 5
    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. – Elijah Lynn Dec 08 '15 at 20:09
  • See [this answer](http://stackoverflow.com/a/30031904/158074) on how to actually make zsh stop expanding invalid brace expressions. – rsenna Nov 29 '16 at 20:24
  • FYI, the error I get from PowerShell for this is "unknown option: -encodedCommand". And surrounding it with double quotes works. – nthpixel Sep 01 '17 at 16:39
  • @XavierPoinas, your code example is very treacherous. Why don’t you replace 'drop' into 'apply'? ("Lifeline" just for inattentive person, as me - https://stackoverflow.com/a/70871838/3240345) – InaFK Jan 31 '23 at 18:33
25

Hi there I had the same thing happen to me. Easiest way of fix it was:

$ git stash apply stash@"{2}"

I'm using a windows git shell.

Martin
  • 3,960
  • 7
  • 43
  • 43
d.f
  • 251
  • 3
  • 2
2

Simply put stash id between simple quotes:

git stash apply 'stash@{1}'
Adriano
  • 487
  • 4
  • 8
  • 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 problem – Brad Mar 08 '17 at 19:59
  • 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 matter – Adriano Mar 09 '17 at 15:19
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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. – DimaSan Mar 09 '17 at 22:51
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

Sergey
  • 19,487
  • 13
  • 44
  • 68
1

For zsh users:

$ git stash apply stash@'{'1'}'
user3251328
  • 188
  • 1
  • 8