如何逃避{in git alias

时间:2018-06-01 15:22:31

标签: bash git escaping git-alias

我正在尝试写日志别名

git config --global --replace-all alias.lol6 "! f() { echo "\$1"; git --no-pager log --oneline  --graph -15 \${@}; }; f"

并像这样使用它:

git lol5 '@{-1}'
git log '@{-1}'  #works

但是传递给git的commit-ish是@ -1

fatal: ambiguous argument '@-1': unknown revision or path not in the working tree

我看过this post但不了解如何使用

由于 波阿斯

更多信息,尝试@vonc建议

git config --global --replace-all alias.lol6 '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'

得到相同的结果,打开GIT_TRACE

git lol6 @{-1}
20:20:37.454153 git.c:576               trace: exec: git-lol6 '@{-1}'
20:20:37.454153 run-command.c:640       trace: run_command: git-lol6 '@{-1}'
20:20:37.463150 run-command.c:640       trace: run_command: ' f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f' '@{-1}'
@-1
22:20:37.607150 git.c:344               trace: built-in: git log --oneline --graph -15 @-1
fatal: ambiguous argument '@-1': unknown revision or path not in the working tree.

Git正在添加''围绕第一个论点

但是!!!,如果我试过

git lol6 HEAD
20:30:35.621257 run-command.c:640       trace: run_command: ' f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f' HEAD

Git没有添加''在HEAD附近

1 个答案:

答案 0 :(得分:0)

我更喜欢使用字符串引号'而不是双引号:在你的情况下更少逃避。

请参阅此示例,在git bash会话中完成。

vonc@voncavn7:/mnt/d/git/git$ 
git config --global --replace-all alias.lol6 \
  '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'

首先,我检查一下我是否可以打印一个虚假的论点:

vonc@voncavn7:/mnt/d/git/git$ git config --global --replace-all alias.lol6 '! f() { echo "$1"; git --no-pager log --oneline  --graph -15 ${@}; }; f'
vonc@voncavn7:/mnt/d/git/git$ git lol6 e
e
fatal: ambiguous argument 'e': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

然后我在调用别名时尝试,没有任何引号: git lol6 @{-1}

vonc@voncavn7:/mnt/d/git/git$ git lol6 @{-1}
@{-1}
* 29533fb168 (tmp2) RelNotes: the eleventh batch
*   dc8fb4dd7e Merge branch 'rb/quick-install-doc'
|\
| * 65289e9dcd install-doc-quick: allow specifying what ref to install

如果上面的别名/命令不起作用,请务必使用simplified PATH仔细检查:

set G=c:\path\to\latest\git
set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin
set PATH=%PATH%;C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\

OP Boaz Nahum确认in the comments

  

使用&#39;简化路径&#39; - 工作。
  现在意味着什么?为什么会这样?

它的工作原理是因为没有告诉原始路径中的内容,C:\Windows\System32以及 Git之前放置的其他文件夹,并且掩盖了某些Git可执行文件。

  

如何使标准Git安装工作呢?

只需用您自己的Git当前安装路径替换上面的c:\path\to\latest\git

然后设置PATH

set PATH=%G%\bin;%G%\usr\bin;%G%\mingw64\bin

接下来,从原始%PATH%添加所有其他文件夹,始终C:\windows\system32;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\结尾。
那样,你:

  • 从最具体的开始(Git:git/bingit/usr/bingit/ming64/bin),确保Git按预期工作
  • 继续使用更常规的应用程序路径(您在原始路径中使用的路径)
  • 以最常用的PATH结束:操作系统Windows窗口(C:\windows\system32\...个)

从最具体到最一般的,你降低&#34;意想不到的行为&#34;已安装的软件。