Git alias with shell variable extension

I would like to use the shell variable extension inside the git alias to remove the branch prefix. Unfortunately, when I use the "standard" alias, the variable extension does not execute:

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

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

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

it starts via sh and does not perform the required replacement. Is there a workaround?

+4
source share
2 answers

Try to change

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

to

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

To execute commands and sed , sh backticks are used to perform regular expression replacements.

+1
source

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

0
source

Source: https://habr.com/ru/post/1383623/


All Articles