3

I'm working on the following git alias

[alias]
  pushup = !git push --set-upstream origin `git branch | egrep "^\*" | awk -F"*" '{print $NF}'`

which is supposed to do a git push while simultaneously setting the upstream to the current branch.

The command

git push --set-upstream origin `git branch | egrep "^\*" | awk -F"*" '{print $NF}'`

works fine by itself on the command-line but I get a bad config file error when I try to use it as a git alias.

Clearly I don't understand something about the format of git aliases. Anyone want to enlighten me?

Mark Biek
  • 146,731
  • 54
  • 156
  • 201

2 Answers2

1

I'm not sure what went wrong, but the following simplified alias works for me:

[alias]
    pushup = !git push --set-upstream origin $(git branch | awk '/^*/{print $2}')

Instead of grepping for * and then splitting on it, you could rely on awk's splitting on whitespace to automatically get you the branch name as the second field, and use awk do grep's job as well.

Vim highlights \* as an error, and when I try with it, I get the error you got (I'm using echo for debugging :D):

enter image description here

So that's the cause of the error. I still don't know why.

I suppose you could also use the simpler command from this SO post:

[alias]
    pushup = !git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)
Community
  • 1
  • 1
muru
  • 4,723
  • 1
  • 34
  • 78
  • Great answer. Thanks for A.) telling about using awk to do the grep part, and B.) a much tidier way to get the name of the current branch. – Mark Biek Feb 13 '16 at 19:32
0

The reason for the error is that \ is recognized as an escaping character while parsing the alias. The parser expects something that needs escaping after the \, but the asterisk doesn't fit the bill.

You should escape the \ with a second backslash to make the parser happy and pass it along to egrep. Like this:

[alias]
  pushup = !git push --set-upstream origin `git branch | egrep "^\\*" | awk -F"*" '{print $NF}'`
seabeast
  • 181
  • 4