我的Git别名有什么问题

时间:2013-10-28 21:36:42

标签: git alias

我想让git的别名从本地和远程存储库中删除分支。所以,我在~/.gitconfig创建了一个:

[alias]
    erase = !"git push origin :$1 && git branch -D $1"

它按预期工作,从原点和本地删除分支但在控制台中我看到额外的行(error: branch 'profile_endpoints' not found.):

┌[madhead@MADHEAD-LAPTOP:/c/projects/b developing]
└─$ git erase profile_endpoints
To git@github.com:a/b.git
 - [deleted]         profile_endpoints
Deleted branch profile_endpoints (was abcdef0).
error: branch 'profile_endpoints' not found.

我在Windows 7上使用git version 1.8.0.msysgit.0git bash

我错过了什么?

1 个答案:

答案 0 :(得分:2)

问题是当你运行一个git别名时,git会对字符串末尾的参数进行处理。试试,例如:

[alias]
    showme = !echo git push origin :$1 && echo git branch -D $1

然后运行:

$ git showme profile_endpoints
git push origin :profile_endpoints
git branch -D profile_endpoints profile_endpoints

有各种解决方法。一个微不足道的是假设将给出一个将附加的参数,所以:

[alias]
    showme = !echo git push origin :$1 && echo git branch -D

然而,这个版本增加了滥用的危险:

$ git showme some oops thing
git push origin :some
git branch -D some oops thing

另一个标准技巧是定义一个shell函数,以便传递所有附加的参数:

[alias]
    showme = !"f() { case $# in 1) echo ok, $1;; *) echo usage;; esac; }; f"

$ git showme some oops thing
usage
$ git showme one
ok, one

有一点是使用虚拟“吸收额外参数”命令:

[alias]
    showme = !"echo first arg is $1 and others are ignored; :"

$ git showme one two three
first arg is one and others are ignored

我自己的个人规则是,一旦别名变得复杂,就切换到“真正的”shell脚本。 : - )