带有位置参数的 Git 别名 [英] Git alias with positional parameters

查看:35
本文介绍了带有位置参数的 Git 别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想别名:

git files 9fa3

...执行命令:

git diff --name-status 9fa3^ 9fa3

但 git 似乎没有将位置参数传递给 alias 命令.我试过了:

but git doesn't appear to pass positional parameters to the alias command. I have tried:

[alias]
    files = "!git diff --name-status $1^ $1"
    files = "!git diff --name-status {1}^ {1}"

...还有一些其他的,但没有用.

...and a few others but those didn't work.

退化的情况是:

$ git echo_reverse_these_params a b c d e
e d c b a

...我怎样才能做到这一点?

...how can I make this work?

推荐答案

一个 shell 函数可以帮助解决这个问题:

A shell function could help on this:

[alias]
    files = "!f() { git diff --name-status "$1^" "$1"; }; f"

没有!的别名被当作Git命令;例如commit-all = commit -a.

An alias without ! is treated as a Git command; e.g. commit-all = commit -a.

使用 !,它在 shell 中作为自己的命令运行,让您可以像这样使用更强大的魔法.

With the !, it's run as its own command in the shell, letting you use stronger magic like this.

UPD
因为命令是在存储库的根目录下执行的,所以在引用命令中的文件名时可以使用 ${GIT_PREFIX} 变量

这篇关于带有位置参数的 Git 别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆