设置git别名,但是调用它会给出“找不到命令” [英] set up git alias, but then calling it gives 'command not found'
问题描述
我想在git中设置一个别名来计算存储库中的总行数,所以我进入了Git Bash并输入了以下内容:
I want to set up an alias in git for counting the total lines in a repository, so I went into Git Bash and entered this:
git config --global alias.linecount 'ls-files -z | xargs -0 wc -l'
输入命令后,没有错误消息。然后我输入了
After I entered the command, there was no error message. Then I entered
linecount
并收到以下错误消息:
sh: linecount: command not found
是否应该以其他方式设置别名?
Is there a different way that I should be setting up an alias?
推荐答案
您缺少感叹号(!
)。
发件人: man git-config
:
如果别名扩展名带有前缀
If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.
您可以这样做:
git config alias.linecount 'ls-files -z'
这是 git
命令的别名,但是由于您使用的是shell语法(如管道),因此其所有参数都由<$ c $解释c> git 它本身,因此在这种情况下,您需要弄清楚应将其视为shell命令。
which would be an alias to git
command, however since you're using shell syntax (like pipe), then all its parameters are interpreted by git
it-self, so in this case you need to clarify that this should be treated as a shell command.
这里是示例在〜/ .gitconfig
中的git别名:
Here is sample git alias in ~/.gitconfig
:
[alias]
linecount = !git ls-files -z | xargs -0 wc -l
在命令行中,正确的语法为:
From the command-line, the correct syntax would be:
git config alias.linecount '!git ls-files -z | xargs -0 wc -l'
注意:添加-全局
是可选的。
Note: Adding --global
is optional.
然后您通过以下方式调用它:
Then you call it by:
git linecount
这篇关于设置git别名,但是调用它会给出“找不到命令”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!