如何直接在 git 别名中嵌入 bash 脚本

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

How to embed bash script directly inside a git alias

gitbashshellalias

提问by Seba Illingworth

Can I embed the following bash shell code:

我可以嵌入以下 bash shell 代码:

for name in $(git diff --name-only ); do git difftool  $name & done

directly into the creation of a git alias:

直接进入git别名的创建:

git config --global alias.diffall ***my-bash-code-here***

This leads on from my previous question/answeron SO, where I put the code into a .sh file and then aliased to the file:

这源于我之前关于 SO 的问题/答案,我将代码放入 .sh 文件中,然后别名到该文件:

git config --global alias.diffall '!sh diffall.sh'

But in the never-ending quest for simplicity, there's gotta be a way to skip the file and insert code directly into the alias? I can't figure out the format...

但是在对简单性的永无止境的追求中,必须有一种方法可以跳过文件并将代码直接插入别名中吗?格式看不懂。。。

回答by u0b34a0f6ae

git config --global alias.diffall '!sh diffall.sh'

This is redundant in one way. If you are going to add 'diffall.sh' into your $PATH anyway, why not save it as 'git-diffall', and save yourself from declaring an alias. Yes, "git diffall" will run it.

这在一方面是多余的。如果您无论如何都要将“diffall.sh”添加到您的 $PATH 中,为什么不将其另存为“git-diffall”,并避免声明别名。是的,“git diffall”会运行它。

回答by michael

To run commands inside of a git alias, and in particular to pass arguments to those commands, you will likely have to create a temporary function which you then immediately invoke:

要在 git 别名内运行命令,特别是将参数传递给这些命令,您可能必须创建一个临时函数,然后立即调用该函数:

$ vim ~/.gitconfig
...
[alias]
    # compare:
    foo = "! echo begin arg=//end"
    foo2 = "!f() { echo "begin arg=//end"; }; f"

In this example, the function is probably what you need (and is also more flexible as to what you can do in a single "statement"); and you can probably tell that for both options, the remaining args to the git command are simply passed as args to the alias, regardless if it's "echo" or "f"; invoking the function simply consumes the args, ignoring what's not explicitly used:

在这个例子中,这个函数可能就是你所需要的(并且对于你可以在单个“语句”中做什么也更加灵活);您可能会发现,对于这两个选项,git 命令的其余参数只是作为参数传递给别名,无论它是“echo”还是“f”;调用函数只会消耗参数,忽略未显式使用的内容:

$ git foo a b c
begin arg=a/b/end a b c

$ git foo2 a b c
begin arg=a/b/end

Another example (lists all aliases, based on matching pattern) (note: you can keep reusing the same function name "f()" throughout the .gitconfig):

另一个示例(根据匹配模式列出所有别名)(注意:您可以在整个 .gitconfig 中重复使用相同的函数名称“f()”):

[alias]
    alias = "!f() { git config --get-regexp "^alias.$" ; }; f"

The first returns the alias for just "foo$", the second for "foo.*":

第一个返回“foo$”的别名,第二个返回“foo.*”:

$ git alias foo
alias.foo ! echo begin arg=//end

$ git alias 'foo.*'
alias.foo ! echo begin arg=//end
alias.foo2 !f() { echo begin arg=//end; }; f

(nb: actual results may vary based on shell; I'm using this with bash on Linux, Unix & Cygwin (Windows).)

(注意:实际结果可能因 shell 而异;我在 Linux、Unix 和 Cygwin (Windows) 上将其与 bash 一起使用。)

回答by albfan

I couldn't find in documentation, but if you create a script "git-<name>" in path, you can call it with "git name" in your repo.

我在文档中找不到,但是如果你在路径中创建一个脚本“git-<name>”,你可以在你的仓库中用“git name”来调用它。

See:

看:

$ cd ~/bin
$ echo "echo I love this log:
>pwd
>git log --graph  --summary --decorate --all" > git-logg
$ chmod +x git-logg
$ cd /path/to/your/repo
$ git logg
I love this log:
/path/to/your/repo
* commit 3c94be44e4119228cc681fc7e11e553c4e77ad04 (whatever-branch)
| Author: myself <my@Laptop.(none)>
| Date:   Fri Apr 1 16:47:20 2011 +0200
| 
|     would have been better not to do it at all
| 
...
$

So, you can write any alias you like with this (rather obscure) way too.

因此,您也可以使用这种(相当晦涩的)方式编写您喜欢的任何别名。

Even more you can add autocompletion to that new command defining a function. Info here

您甚至可以向定义函数的新命令添加自动完成功能。信息在这里

$ _git_logg ()
{
  # you can return anything here for the autocompletion for example all the branches
  __gitcomp_nl "$(__git_refs)" 
}

回答by David Claridge

Adding these 2 line to your .git/config file should do the trick.

将这两行添加到您的 .git/config 文件应该可以解决问题。

[alias]
    diffall = '!for name in $(git diff --name-only ); do git difftool  $name & done'

Edit: presumably the git-config version works too, but I like to keep my aliases in the config file for ease of management.

编辑:大概 git-config 版本也可以工作,但我喜欢在配置文件中保留我的别名以便于管理。

There is a nice page on the git wiki that explains aliases very clearly: http://git.or.cz/gitwiki/AliasesIn particular, read 'advanced aliases with arguments'

git wiki 上有一个很好的页面,它非常清楚地解释了别名:http: //git.or.cz/gitwiki/Aliases特别是,请阅读“带参数的高级别名”

回答by Denian

(From ProGit documentation: http://progit.org/book/ch7-3.html)

(来自 ProGit 文档:http://progit.org/book/ch7-3.html )

Have you tried to add a script in .git/hooks?

您是否尝试在 .git/hooks 中添加脚本?

For example, if you create a script .git/hooks/post-checkout:

例如,如果您创建一个脚本 .git/hooks/post-checkout:

#!/bin/bash

echo "This is run after a 'git checkout'"

and then run the command:

然后运行命令:

$ git checkout master
Switched to branch 'master'
This is run after a 'git checkout'