4

I'd like to use a shell variable expansion inside of a git alias to strip a prefix of a branch. Unfortunately when I use the "standard" alias, variable expansion is not done:

publish = push -u origin ${$(git symbolic-ref HEAD)##refs/heads/}

This is trying to actually push a branch called "${$(git". But if I change the alias to:

publish = "!git push -u origin ${$(git symbolic-ref HEAD)##refs/heads/}"

it's run via sh and fails to do the substitution I want. Is there some workaround?

viraptor
  • 33,322
  • 10
  • 107
  • 191

2 Answers2

1

Try changing

!git push -u origin ${$(git symbolic-ref HEAD)##refs/heads/}

to

!git push -u origin `git symbolic-ref HEAD | sed -e "s#^refs/heads/##"`

This uses sh backticks to execute commands and sed to do the regular expression substitution.

Go Dan
  • 15,194
  • 6
  • 41
  • 65
  • @viraptor mmmm if that's the solution, I'd seriously consider just delegating to a bash script proper and not bother with any such archaics :) In a bash script, there will be no surprises and unlimited options. – sehe Nov 29 '11 at 21:59
1

Low-level explanation: The ${xxx} syntax always requires a variable name for xxx. It does not substitute an arbitrary string. To use ##, one would have to: x=$(git symbolic-ref HEAD); echo ${x##refs/heads/};

jørgensen
  • 10,149
  • 2
  • 20
  • 27