使用管道命令时,Git别名返回错误 [英] Git alias returns an error when using pipe command

查看:150
本文介绍了使用管道命令时,Git别名返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 git-ftp ,并且正在尝试执行 awk 仅包含第一行:

I'm using git-ftp and I'm trying to execute awk to only include the first lines:

git ftp show | awk 'NR<7'

它在终端机上非常完美。但是,将其作为别名执行将返回错误。

It works perfect in terminal. However executing it as an alias returns an error.

这是我的git配置文件的样子:

This is how my git config file looks like:

[alias]
sh = ftp show | awk 'NR<7'

如果我运行 git sh 它返回:致命:无法识别的选项:awk

我也尝试只使用show,但是它还返回错误:

I also tried just using show, but it also returns an error:

[alias]
sh = show | awk 'NR<7'

致命:致命的参数'|':未知的修订版或路径

可以以某种方式设置此命令的格式以使其用作git别名吗?

Can this command be formated in some way to make it work as a git alias?

推荐答案

要注意的第一件事是,git别名仅在以bang(感叹号)为前缀时才与shell命令一起使用。
我没有使用git-ftp,所以我无法测试您的精确命令,但是我使用了 git remote show origin ,因为它在其中之一上非常冗长我的回购。
我发现以下git别名可以正常工作: sh =!git remote show origin | awk'NR< 7'
我因此希望git alias为您提供解决方案:

The first thing to note is that git aliases only work with shell commands when prefixed with a bang (exclamation mark). I'm not using git-ftp so I can't test your precise command, but I used git remote show origin instead because it's very verbose on one of my repos. I found the following git alias worked fine: sh = "!git remote show origin | awk 'NR<7'" I therefore expect this git alias to be a solution for you:

sh = "!git ftp show | awk 'NR<7'"

意识到有些爆炸/竖笛/引号组合用作git别名,而另一些则会失败,可能是致命配置行错误。
最常见的真正问题是如何使用引号。诀窍可以是仅更改您的特定git别名shell命令行以消除或更改引用用法。
例如,我有一个git别名的要求,我通过将 sed 命令更改为 tr 来解决报价问题。 code>。然后,我不得不使用 ... |来加倍转义反斜杠。 tr \n

Realise that some bang/pipe/quote combinations work as git aliases, while others will fail, possibly with "fatal bad config line". Most often the real problem is how quotes are used. The trick can be to just change your particular git alias shell command line to eliminate or change quote usage. For instance one requirement for a git alias that I had, I worked around the quote problem by changing a sed command to tr. I then had to double escape a backslash using: ...| tr \\\\n.

对于这个特定问题,尽管我不认为git-alias有问题用引号引起来,更安全的报价解决方案是:

For this particular question, although I don't think git-alias has a problem with the quotes, a more quote-safe solution would be:

sh = ! git ftp show | head -6

虽然大多数读者可能已经知道爆炸的要求,但他们可能没有意识到报价是一个更困难的问题。
尽管并非完全正确,但我发现本文对提出建议很有用: https://www.atlassian.com/blog/git/advanced-git-aliases

While most readers may already know about the requirement to shell out with a bang, they may not realise that quoting is a more difficult issue. Although not entirely correct, I found this article was useful for ideas: https://www.atlassian.com/blog/git/advanced-git-aliases

这篇关于使用管道命令时,Git别名返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆