git - 如何编写一个以字符串作为参数的 git 别名

标签 git zsh git-alias git-grep

我想为以下内容编写一个 git 别名:

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

到目前为止我所拥有的是:

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

它完美地回显了字符串,但给出了错误,我似乎不知道如何将字符串传递给 grep 标志。

$ 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>...]'

我正在使用 zsh,如果它有什么区别的话。 。 .

最佳答案

如果您使用双引号,该别名似乎可以工作:

git search "Big boi"

我还使其与 --grep=\"$str\" 一起使用(并且仍然使用双引号)

OP joshuatvernon添加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.

关于git - 如何编写一个以字符串作为参数的 git 别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49040686/

相关文章:

githooks - 如何让 git 将分支合并回父分支

git - 来自 Git 别名的 Bash 中的错误变量名

git - 如何恢复 Git 中丢失的存储?

git - 使用 Mergetool 比较和编辑 Git 中的分支

git - 使用 CopSSH 和 MSysGit 从 Windows 克隆 git 存储库

ruby - ri 不在 zsh 中工作

terminal - Urxvt 中使用 Zsh 动态窗口标题?

git - 撤消对单个文件的更改而不显示在历史记录中

zsh - setopt listambiguous 不设置选项

Git 配置别名转义