1

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.

Community
  • 1
  • 1
MikeF
  • 764
  • 9
  • 26

1 Answers1

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
Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • Thanks, I don't get the bad config, but the `*` also does not get passed to grep, so I don't get the current branch – MikeF Apr 27 '17 at 12:58
  • 1
    Right, you want the *shell* to see the two character sequence backslash, star. Git "eats" one backslash (and demands that the character after it be something Git would treat specially)—it uses them to protect quotes, for instance, so that if you want to get one double quote to the shell you write `\"`—so to get one backslash through, you write two backslashes. The shell then sees `\*` and "eats" one backslash while protecting `*` from expanding to all file names. – torek Apr 27 '17 at 14:39
  • 1
    Note that if you wanted to get one backslash through to `grep`, you would have to write *four* backslashes: `... | grep \\\\ | ...` since Git would eat one of each pair, leaving two, and then the shell would eat one, leaving just one backslash. If you use a command like `printf` that interprets backslash and you want to get one backslash through, you now need *eight* backslashes: `... printf %s \\\\\\\\t ...` to print `\t` rather than an actual tab! – torek Apr 27 '17 at 14:42
  • 1
    This need to keep doubling, every time we pas through another layer of interpretation, is why we have other ways to quote things. So you could just write `thisbranch2 = !git branch | grep '*' | cut -d' ' -f2`, for instance. – torek Apr 27 '17 at 14:45