添加包含分号的Git别名

Mec*_*ail 8 git parsing git-config

当我尝试创建别名时

[alias]
    my-alias = submodule foreach 'git foo ; git bar'
Run Code Online (Sandbox Code Playgroud)

Git(版本1.7.1)发出错误

user@host:/path/repo.git$ git my-alias
error: unclosed quote
fatal: Bad alias.my-alias string
Run Code Online (Sandbox Code Playgroud)

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

如何指定此别名?

Mec*_*ail 12

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

my-alias = "submodule foreach 'git foo ; git bar'"
Run Code Online (Sandbox Code Playgroud)

双引号使.gitconfig解析器传递分号.仍需要单引号来界定参数submodule foreach; 没有它们,它会被解析为

submodule foreach 'git foo'
git bar
Run Code Online (Sandbox Code Playgroud)

所以最后git bar才会执行一次.


sda*_*aau 5

不知道这是否与分号有关,但是可以了-这是使用git别名的另一项测试bash

[alias]
        testbash = "!bash -c \"ix=1; echo a\\$ix\""
Run Code Online (Sandbox Code Playgroud)

测试:

$ git testbash 
a1
Run Code Online (Sandbox Code Playgroud)

任何其他形式的转义都使我要么是普通的“致命的:错误的配置文件行”,要么是“未终止的带引号的字符串”或“意外的EOF”(另请参见shell-使用从args读取的命令从sh(破折号)调用bash,以及“带引号的未终止字符串“ /”意外的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 '"
Run Code Online (Sandbox Code Playgroud)

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

[alias]
  testbashm3 = "!bash -c 'ix=1; echo a$ix; \n\
    #echo b$ix ; \n\
    echo \"c$ix\" ; '"
Run Code Online (Sandbox Code Playgroud)