Git 别名 - 多个命令和参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7534184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git Alias - Multiple Commands and Parameters
提问by Stella
I am trying to create an alias that uses both multiple Git commands and positional parameters. There are Stackoverflow pages for each, and it would appear painfully obvious to do both, but I am having trouble.
我正在尝试创建一个同时使用多个 Git 命令和位置参数的别名。每个页面都有 Stackoverflow 页面,两者都执行似乎很明显,但我遇到了麻烦。
As an example, I want to switch to branch foo and perform a status. So in my .gitconfig
, I have:
例如,我想切换到分支 foo 并执行状态。所以在我的.gitconfig
,我有:
[alias]
chs = !sh -c 'git checkout chs = !sh -c 'git checkout [alias] chs = !git checkout && git status
'
echoes = !sh -c 'echo hi && echo bye'
&& git status'
which doesn't work. Whereas something like this will work.
这不起作用。而像这样的事情会起作用。
[alias] chs = !git checkout && git status && :
Any insight would be appreciated.
任何见解将不胜感激。
回答by Olivier Verdier
This will work (tested with zsh and bash):
这将工作(用 zsh 和 bash 测试):
[alias] chs = "!f(){ git checkout \"\" && git status; };f"
回答by Brondahl
This targets Windows batch / msysgit bash; might not work on other environments.
这针对 Windows 批处理/msysgit bash;可能不适用于其他环境。
As Olivier Verdier and Lily Ballard have said
正如 Olivier Verdier 和 Lily Ballard 所说
[alias] chs = !git checkout $1 && git status
[alias] chs = !git checkout $1 && git status
almost works, but gives a spurious extra insertion of the argument ...
几乎有效,但给出了一个虚假的额外插入参数......
git chs demo -> git checkout demo && git status demo
git chs demo -> git checkout demo && git status demo
But if you add && :
to the end of your alias, then the spurious argument is consumed into a location tag.
但是,如果您添加&& :
到别名的末尾,那么虚假参数将被消耗到位置标记中。
So
所以
safereset = "!f() { \
trap 'echo ERROR: Operation failed; return' ERR; \
echo Making sure there are no changes...; \
last_status=$(git status --porcelain);\
if [[ $last_status != \"\" ]]; then\
echo There are dirty files:;\
echo \"$last_status\";\
echo;\
echo -n \"Enter Y if you would like to DISCARD these changes or W to commit them as WIP: \";\
read dirty_operation;\
if [ \"$dirty_operation\" == \"Y\" ]; then \
echo Resetting...;\
git reset --hard;\
elif [ \"$dirty_operation\" == \"W\" ]; then\
echo Comitting WIP...;\
git commit -a --message='WIP' > /dev/null && echo WIP Comitted;\
else\
echo Operation cancelled;\
exit 1;\
fi;\
fi;\
}; \
f"
gives the correct output ...
git chs demo -> git checkout demo && git status
给出正确的输出...
git chs demo -> git checkout demo && git status
回答by Lily Ballard
You can define a shell function.
你可以定义一个shell函数。
[alias]
chs = !git branch && git status
回答by VitalyB
I was able to create multi-line and quite complex git aliases. They work fine on Windows but I assume they'd work elsewhere too, for example:
我能够创建多行且非常复杂的 git 别名。它们在 Windows 上运行良好,但我认为它们也可以在其他地方运行,例如:
[alias]
chs = "!sh -c 'git checkout \"[alias]
chs = "!git checkout \
; git status \
"
\" && git status'"
I wrote a post and have a few more examples here.
我写了一篇文章,这里还有一些例子。
回答by FractalSpace
[alias]
test = !git echo $*
回答by brocksamson
Try this one:
试试这个:
03:41:24 (release) ~/Projects/iOS$ git test this is my testing string
this is my testing string this is my testing string
^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^
#1 #2
Call it like this: git chs master
像这样调用它: git chs master
回答by gmarik
It's possible to have multiline git alias by appending \
at the end of each line.
通过\
在每行的末尾附加,可以拥有多行 git 别名。
[alias]
chs = !git checkout && git status && git echo x >/dev/null
回答by Ben Collins
The problem here is that the positional parameters seem to be getting sent to the shell command twice (as of git 1.9.2). To see what I mean, try this:
这里的问题是位置参数似乎被发送到 shell 命令两次(从 git 1.9.2 开始)。要明白我的意思,试试这个:
##代码##Then, do git test this is my testing string
. You should observe the following output (last two lines edited here for clarity):
然后,做git test this is my testing string
。您应该观察以下输出(为清楚起见,在此处编辑了最后两行):
One way to work around this would be to
解决此问题的一种方法是
##代码##This will consume the extra positional parameter as it gets applied to that last echo command and have no effect on the results.
这将消耗额外的位置参数,因为它被应用于最后一个 echo 命令并且对结果没有影响。