1

I want to make an alias to drop a git stash in zsh shell. The stash no. which I want to drop should be passed as an argument to my function call.

I have tried below but it is failing -

function gd() {
    if [ -n "$1" ]
    then
        git stash drop "$1"
    else
        echo 'Enter stash no to drop'
    fi
}

It gives me below error -

fatal: ambiguous argument '0': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Seems I am not passing the argument correctly and it is being treated as a string.

How can I make this work?

Shantanu Tomar
  • 1,572
  • 7
  • 38
  • 64

2 Answers2

1

@ShantanuTomar : You don't define any alias, which is not a bad thing in the first place, because a function is more flexible anyway, but if you really want to have an alias, the command to define it would be

 alias gd='git stash drop'

Aside from this, your function definition is fine, though you don't need to quote your variables. It doesn't harm doing so, though.

The error message says that the stash you provided, does not exist. Use

git stash list

to get a list of the available stashes.

user1934428
  • 19,864
  • 7
  • 42
  • 87
-1

Try

git stash drop $1

But, as commented, there won't be any kind of conversion done by zsh alone.

So, make sure to use a recent enough Git:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `$1` is a string either way. Quoting is necessary if the branch name contains characters that the shell might process before `git` ever sees it; it doesn't do any kind of type conversion. – chepner Dec 08 '19 at 22:14
  • @chepner: Not in this case, because we have Zsh. Even if $1 would contain wildcards, they would not be evaluated if unquoted. – user1934428 Dec 09 '19 at 10:13
  • That depends on your shell settings. Either way, though, not quoting doesn't make the shell treat it as an integer, because even `zsh` doesn't have an integer data type: it's all strings. – chepner Dec 09 '19 at 13:13