每当我尝试输入git stash list
时,我的手指都会流氓并输入git stash ls
。我真的想将ls别名列入清单,但仅限于stash子命令。使用git别名可以吗?
答案 0 :(得分:7)
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 list
和git
之间存在其他命令行参数,则stash ls
将不运行,如
git -C ~/Desktop stash ls
但这种方法应该足以满足您的使用需求。
为避免每次启动shell时都必须重新键入该git
函数的定义,应将其放在shell配置为源的其中一个文件中。