How do I expand a built in Git command with an alias?

痞子三分冷 提交于 2019-12-11 00:48:24

问题


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).



来源:https://stackoverflow.com/questions/11226066/how-do-i-expand-a-built-in-git-command-with-an-alias

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