13

当我尝试创建别名时

[alias]
    my-alias = submodule foreach 'git foo ; git bar'

Git(版本 1.7.1)喷出错误

user@host:/path/repo.git$ git my-alias
error: unclosed quote
fatal: Bad alias.my-alias string

似乎.gitconfig使用了奇怪的解析规则,因此;即使在引号内,也将其视为开始行注释。

如何指定此别名?

4

6 回答 6

16

将整个别名命令用双引号括起来:

my-alias = "submodule foreach 'git foo ; git bar'"

双引号导致.gitconfig解析器传递分号。仍然需要单引号将参数分隔为submodule foreach; 没有它们,它会被解析为

submodule foreach 'git foo'
git bar

所以git bar最后只执行一次。

于 2012-02-28T08:31:05.803 回答
5

不确定这是否与分号有关,但这里是 - 这是 git 别名的另一个测试,使用bash

[alias]
        testbash = "!bash -c \"ix=1; echo a\\$ix\""

测试:

$ git testbash 
a1

任何其他形式的转义都会给我普通的旧“致命:错误的配置文件行”,或“未终止的引用字符串”或“意外的 EOF”(另请参见shell - Calling bash from sh (dash) with commands read from args, and “未终止的带引号的字符串“/”意外的 EOF” - Unix & Linux Stack Exchange )

也适用于多行:

[alias]
  testbashm1 = "!bash -c \"ix=1; echo a\\$ix; \
echo b\\$ix \""
  testbashm2 = "!bash -c 'ix=1; echo a$ix; \
echo b$ix '"

...并添加\n\到行尾,如果您想使用内联bash注释 ( #):

[alias]
  testbashm3 = "!bash -c 'ix=1; echo a$ix; \n\
    #echo b$ix ; \n\
    echo \"c$ix\" ; '"
于 2013-04-21T22:10:28.563 回答
2

您需要使用双引号 ( ") 而不是单引号 ( ')。

[alias]
    foo = "submodule foreach 'echo foo; echo bar'"
    bar = submodule foreach 'echo foo; echo bar'

$ git foo
foo
bar
$ git bar
fatal: Bad alias.bar string: unclosed quote
于 2012-02-28T08:26:40.027 回答
2

为了获得完全的灵活性,定义并调用一个函数:

[alias]
    conf = ! "                                \
        f () {                                \
            git config \"$@\" --get-regexp .  \
            | sort;                           \
        };                                    \
        f"

这个别名可以被称为git conf,git conf --localgit conf --global,并且额外的选项被插入到适当的位置。

于 2016-04-22T17:32:40.640 回答
1

只需将命令包装成双引号,例如:

foo       = !"echo foo; echo bar"

要包含分号find,请对其进行双重转义,例如:

pull-all  = !"find . -name .git -type d -print -execdir git pull origin \\;"

与您的命令相同:

my-alias  = "submodule foreach 'git foo; git bar'"

对于故障排除,请在命令前加上GIT_TRACE=1调试别名,例如

$ GIT_TRACE=1 git my-alias
18:16:07.904421 git.c:282               trace: alias expansion: my-alias => 'submodule' 'foreach' 'git foo; git bar'
18:16:07.904437 git.c:557               trace: exec: 'git-submodule' 'foreach' 'git foo; git bar'
18:16:07.904443 run-command.c:347       trace: run_command: 'git-submodule' 'foreach' 'git foo; git bar'
于 2016-03-23T18:14:05.923 回答
0

我有

[alias]
  sm-clean-all = "submodule foreach --recursive 'git clean -fXd'"
于 2013-01-17T10:31:10.417 回答