How do I expand a built in Git command with an ali

2019-07-16 10:15发布

问题:

When answering "Git pull hook script which executes locally", I stumbled upon the use-case to alias a built-in git command such as pull or push with an extension. How do I do that?

First thought was:

[alias]
    push = "!echo -n \"really push? [y/n]\";read -s -n 1 really;if [[ $really == \"y\" ]];then git push; else echo \"aborting\"; fi"

This works fine as long as I don't name my alias push (for example qp or something like that). But as soon as I call it push, it's somehow ignored.

Is there a git way to expand a built in git command with an alias or do I have to set up an alias in my .bashrc?

回答1:

Short answer: you can't.

Git disallows this explicitly to prevent confusion and shadowing that might affect the invocation of git commands (in scripts, etc.). See the git-config manpage.

alias.*

Command aliases for the git(1) command wrapper - e.g. after defining "alias.last = cat-file commit HEAD", the invocation "git last" is equivalent to "git cat-file commit HEAD". To avoid confusion and troubles with script usage, aliases that hide existing git commands are ignored. Arguments are split by spaces, the usual shell quoting and escaping is supported. quote pair and a backslash can be used to quote them.

You could, as you noted, just name your alias something else and use that instead, or do it in bash. However, note that multiword aliases in bash are not possible, so you can't have an alias for "git push". Instead you'll need to use a function -- see "Bash: Spaces in alias name" on SuperUser for some hints (you can probably adopt it wholesale).



标签: git alias