每当我尝试输入
git stash list
时,我的手指就会误按并键入git stash ls
。我真的很想将ls
别名为list
,但仅在stash子命令中使用。是否可以使用git别名实现这一点?git stash list
时,我的手指就会误按并键入git stash ls
。我真的很想将ls
别名为list
,但仅在stash子命令中使用。是否可以使用git别名实现这一点?Git没有提供任何子命令的别名机制;请参阅git-config
手册页面。
然而,有一种方法可以实现相同的效果:通过定义一个名为git
的shell命令来包装git
二进制文件,以执行所需操作:
git() {
if [ "$1" = "stash" -a "$2" = "ls" ]
then
command git stash list
else
command git $@
fi;
}
虽然不太好看,但可以完成任务(在我的一个代码库中进行了测试):
# starting from a dirty working state...
$ git stash save
Saved working directory and index state WIP on master: 7c6655d add missing word in documentation
HEAD is now at 7c6655d add missing word in documentation
$ git stash ls
stash@{0}: WIP on master: 7c6655d add missing word in documentation
请注意,这种方法并不是非常健壮。特别地,在git
和stash ls
之间有其他命令行参数时,git stash list
将不会被执行,如下所示:
git -C ~/Desktop stash ls
虽然这种方法对您的用例应该足够,但要避免每次启动shell时重新输入git
函数的定义,您应将其放在其中一个文件中,以便您的shell进行配置。