gitzshgit-aliasgit-grep

How can I write a git alias that takes a string as an argument


I would like to write a git alias for:

git log --all --grep='Big boi'

What I have so far is:

[alias]
    search = "!f() { str=${@}; echo $str; git log --all --grep=$str; }; f"

Which echos the string perfectly fine but gives an error, I can't seem to figure out how to pass the string to the grep flag.

$ user in ~/src/repo on master λ git search 'Big boi'
Big boi
fatal: ambiguous argument 'boi': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

I'm using zsh if it makes any difference . . .


Solution

  • That alias seems to work if you are using double-quotes:

    git search "Big boi"
    

    I also made it work with --grep=\"$str\" (and still using double-quotes)

    The OP joshuatvernon adds in the comments:

    I amended it to

    search = "!f() { str="$*"; echo "$str"; git log --all --grep=\"$str\"; }; f"
    

    and it works with single, double or no quotes.