set up git alias, but then calling it gives 'c

2019-08-08 20:05发布

问题:

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'


标签: git bash alias