git - 如何使用别名扩展内置 Git 命令?

标签 git alias

在回答“Git pull hook script which executes locally”时,我偶然发现了用例为内置 git 命令(例如 pullpush)加上扩展名。我该怎么做?

首先想到的是:

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

只要我不命名我的别名 push(例如 qp 或类似名称),这就可以正常工作。但是我一调用它 push,它就被忽略了。

是否有一种 git 方法可以使用别名扩展内置 git 命令,或者我是否必须在我的 .bashrc 中设置别名?

最佳答案

简短回答:你不能

Git 明确不允许这样做,以防止可能影响 git 命令调用(在脚本等中)的混淆和阴影。参见 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.

如您所述,您可以将别名命名为其他名称并使用它,或者在 bash 中执行。但是,请注意,在 bash 中不可能使用多词别名,因此您不能为“git push”设置别名。相反,您需要使用一个函数——请参阅 SuperUser 上的“Bash: Spaces in alias name”以获取一些提示(您可能会完全采用它)。

关于git - 如何使用别名扩展内置 Git 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11226066/

相关文章:

git - 我如何在 git 中更新我的代码库而不必删除、存储或 checkin 我当前的更改?

sql - 如何在 SQL 中为值列表设置别名

typescript - Typescript 中装饰器的别名

Python奇怪的行为: "+=" creates aliases

git - 如何使用 GIT 命令内联指定 CAFile 路径?

git - 如何更改从中 fork 我的存储库的 GitHub 存储库?

node.js - 如何通过 git 在部署项目上运行 npm? ( Hook /接收后 : npm: command not found)

git - git 在哪里存储最后 checkout 的分支?

sql - 在 oracle 的 join select 查询中将某些列名称别名为一个字段

sql - 如何从 SQL Server 查询中获取列名