git 管道命令时配置错误

git bad config when piping commands

这两个命令都在命令行中工作

git symbolic-ref HEAD | sed -e "s#^refs/heads/##"

git branch | grep \* | cut -d ' ' -f2 

添加到 [alias] 下的 gitconfig 时

thisbranch = !git symbolic-ref HEAD | sed -e "s#^refs/heads/##"
thisbranch2 = !git branch | grep \* | cut -d ' ' -f2

我得到 fatal: bad config line 16 in file /Users/<me>/.gitconfig,这是第二行。由于这个 answer,我最初的问题是将当前分支设置为别名。所以我主要好奇为什么两者都在命令行上工作,但只有 1 个可以在配置中工作。我猜是 ' ' 需要转义,但这只是一个猜测。

你对单引号的使用看起来没问题。

问题是您传递给 grep 的通配符参数导致语法错误。

尝试双重转义通配符:

thisbranch2 = !git branch | grep \* | cut -d ' ' -f2