具有多个命令的 Git 别名的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18860696/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:04:14  来源:igfitidea点击:

Syntax for Git aliases with multiple commands

git

提问by Mike Rylander

I want to create a Git alias to perform multiple commands, but I cant find documentation on how this is done.

我想创建一个 Git 别名来执行多个命令,但我找不到有关如何完成的文档。

  1. What is the syntax for Git aliases with multiple commands?
  2. Where is this documented?
  1. 具有多个命令的 Git 别名的语法是什么?
  2. 这是在哪里记录的?

From 'man git-config'

来自'man git-config'

   alias.*

Command aliases for the git(1) command wrapper - e.g. after defining "alias.last = cat-file commit HEAD", the invocation "git last" is equivalent to "git cat-file commit HEAD". To avoid confusion and troubles with script usage, aliases that hide existing Git commands are ignored. Arguments are split by spaces, the usual shell quoting and escaping is supported. quote pair and a backslash can be used to quote them.

If the alias expansion is prefixed with an exclamation point, it will be treated as a shell command. For example, defining "alias.new = !gitk --all --not ORIG_HEAD", the invocation "git new" is equivalent to running the shell command "gitk --all --not ORIG_HEAD". Note that shell commands will be executed from the top-level directory of a repository, which may not necessarily be the current directory. GIT_PREFIX is set as returned by running git rev-parse --show-prefix from the original current directory. See git-rev-parse(1).

   alias.*

git(1) 命令包装器的命令别名 - 例如,在定义“alias.last = cat-file commit HEAD”之后,调用“git last”等效于“git cat-file commit HEAD”。为避免脚本使用的混淆和麻烦,隐藏现有 Git 命令的别名将被忽略。参数由空格分隔,支持通常的 shell 引用和转义。引用对和反斜杠可用于引用它们。

如果别名扩展以感叹号为前缀,它将被视为一个 shell 命令。例如,定义“alias.new = !gitk --all --not ORIG_HEAD”,调用“git new”相当于运行shell命令“gitk --all --not ORIG_HEAD”。请注意,shell 命令将从存储库的顶级目录执行,该目录不一定是当前目录。通过从原始当前目录运行 git rev-parse --show-prefix 将 GIT_PREFIX 设置为返回。参见 git-rev-parse(1)。

回答by sehe

$ git config alias.q '!echo a; echo b'

$ git q

Output:

输出:

a
b

I think this is (rudimentarily) documented in man git-configunder alias.*

我认为这是(基本)记录man git-config在下alias.*

Note that git commands should include git, unlike in normal aliases. It is caused by fact that it is treated as a shell command, not as a git command (see manpage quoted in the question). For example to chain

请注意,与普通别名不同,git 命令应包含 git。这是因为它被视为 shell 命令,而不是 git 命令(请参阅问题中引用的手册页)。例如链接

git init

and

git commit --allow-empty -m "empty initial commit"

it is necessary to create

有必要创建

"!git init; git commit --allow-empty -m \"empty initial commit\""

alias.

别名。

回答by brita_

Say the commands are echo aand echo b(not aand b), to add multiple commands for an alias q:

假设命令是echo aecho b(不是ab),为一个别名添加多个命令q

From the command line:
git config alias.q '!echo a; echo b'

从命令行:
git config alias.q '!echo a; echo b'

Directly in the configuration file:

直接在配置文件中:

[alias]
    q = "!echo a; echo b"

For more complex things, define a shell function and call it:
'!f() { echo a ; echo b ; }; f'

对于更复杂的事情,定义一个shell函数并调用它:
'!f() { echo a ; echo b ; }; f'

For passing parameters to the commands see:
Git alias with positional parameters
Git Alias - Multiple Commands and Parameters

有关向命令传递参数的信息,请参阅:
Git alias with positional parameters
Git Alias - Multiple Commands and Parameters

Based in Jonathan Wakely's comment

基于Jonathan Wakely的评论

回答by ikrabbe

Addendum Answer: Often I need more complex commands that decide what to do through positional parameters and branch on the parameters or loop through parameters or input files.

附录答案:通常我需要更复杂的命令来决定通过位置参数和参数分支或循环参数或输入文件执行的操作。

Such commands are too complex for single liners and they are hard to read and edit on one line. But I found a real simple method to do very complex commands from files:

这样的命令对于单行程序来说太复杂了,而且很难在一行上阅读和编辑。但是我发现了一个非常简单的方法来从文件中执行非常复杂的命令:

Assume you have a file called alias/cmdin your repository:

假设您alias/cmd的存储库中有一个名为的文件:

!function f {
    if [ -z "" ]
    then echo "Please give me some argument!" 1>&2
        exit -1
    fi
    echo "Hello "
}; f

then you can simply say

那么你可以简单地说

git config alias.cmd "`cat alias/cmd`"

to define the alias cmdfrom the file and

cmd从文件中定义别名和

git config --get alias.cmd > alias/cmd

to write the defined alias to the file.

将定义的别名写入文件。

回答by Paul

I struggled to find a nice example for myself too. After some research, here's what I came up with:

我也努力为自己找到一个很好的例子。经过一番研究,这是我想出的:

git config --global alias.purge '!f() { if [ $(git ls-remote --heads origin  | wc -l) -eq 1 ]; then git branch -d  && git push origin --delete ; else echo "remote branch not found"; fi } ; f'

Let's break that down from left to right, shall we?

让我们从左到右分解,好吗?

git config --global alias.we know by now. It's the code we need in order to create an alias. Often referred to as [alias] on this site

git config --全局别名。我们现在知道了。这是我们创建别名所需的代码。在本网站上通常称为 [别名]

purgeis the shorthand name I decided to give to my alias

purge是我决定给我的别名的速记名称

'f() {we start to write our function

'f() {我们开始编写我们的函数

if [the start of our if-statement, closing it later on with ]

if [if 语句的开始,稍后用]

git ls-remote --heads origin $1checks if there is a remote branch with the name we supplied. Return value would be nothing if there's no branch, otherwise, it'll return the reference tag. By enclosing it in $( )we make sure it's known to Bash as a command

git ls-remote --heads origin $1检查是否有我们提供的名称的远程分支。如果没有分支,返回值将是空的,否则,它将返回引用标记。通过将它包含在$() 中,我们确保 Bash 知道它是一个命令

| wc -1added as a suffix, converts the return value to 0 if there's no remote branch, 1 if there is

| wc -1作为后缀添加,如果没有远程分支,则将返回值转换为0,如果有远程分支,则转换为1

-eq 1turns the whole enclosed command into a boolean, read as (returnValue == 1)

-eq 1将整个封闭命令转换为布尔值,读取为 (returnValue == 1)

NOTE: because the commands are written on one line, instead of \n behind each, we need to type ;after each command

注意:因为命令写在一行上,而不是每行后面的 \n,我们需要键入; 在每个命令之后

thenis used to announce what our function should do if the boolean returns true

then用于宣布如果布尔值返回 true 我们的函数应该做什么

git branch -d $1will delete the local branch of the parameter we entered

git branch -d $1将删除我们输入的参数的本地分支

&&this logical operator will ensure both commands are carried out

&&这个逻辑运算符将确保两个命令都被执行

git push origin –delete $1will delete the remote branch of the parameter we entered

git push origin –delete $1会删除我们输入的参数的远程分支

elsedoes what the second half of an if-else should do

else执行 if-else 的后半部分应该执行的操作

echo "remote branch not found"is a String I want to return in case there's no remote branch

echo "remote branch not found"是一个字符串,我想在没有远程分支的情况下返回

fiannounces the end of our if-else statement

fi宣布 if-else 语句结束

} ; f'closes our function

}; f'关闭我们的功能

'complex' git alias for my branch named "security"

我的名为“security”的分支的“复杂”git别名