set up git alias, but then calling it gives 'command not found'

大憨熊 提交于 2019-12-11 12:04:32

问题


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

and got this error message:

sh: linecount: command not found

Is there a different way that I should be setting up an alias?


回答1:


You're missing exclamation point (!).

From: man git-config:

If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command.

You could do:

git config alias.linecount 'ls-files -z'

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.

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



回答2:


You set up a git alias, not a shell alias.

You need to use git to run it.

git linecount

You also need to use a shell-exec git alias if you expect to use shell features (like pipes).

git config --global alias.linecount '!git ls-files -z | xargs -0 wc -l'


来源:https://stackoverflow.com/questions/33221033/set-up-git-alias-but-then-calling-it-gives-command-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!