-->

git bad config when piping commands

2019-08-06 13:24发布

问题:

Both of these commands work from the command line

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

and

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

When added to gitconfig under [alias]

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

I get fatal: bad config line 16 in file /Users/<me>/.gitconfig which is the second line. My initial problem was getting the current branch into an alias thanks to this answer. So I am mainly curious why both work on the command line, but only 1 can work in config. I am guessing it's the ' ' needs to be escaped, but that's just a guess.

回答1:

Your usage of single quotes looks fine.

The problem is the wildcard argument you are passing to grepis causing a syntax error.

Try double-escaping the wildcard:

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


标签: git git-alias