Why do I want git push -d remotes/remote-name/topic/branch
?
It's the format that you get in gitk, often I find remote branches in gitk that I want to delete. Right click the branch name in gitk, copy and do something like git nuke remotes/remote-name/topic/branch
.
What I currently have:
echo remotes/origin/epic/T-12345 | awk '{gsub(/^remotes\/[^\/]*\//, "")}{print}'
This works fine, prints epic/T-12345
- it chops the optional beginning of string ^remotes/.*?/
in terms of PCREs which are easier to read.
Problem: When I try to use it in a git alias like so (git-for-windows, running from git-bash):
test1 = "!f() { \
echo ${1} | awk '{gsub(/^remotes\/[^\/]*\//, "")}{print}'; \
}; f"
I get fatal: bad config line 5 in file C:/Users/username/.gitmorealiases
The plan was to do something like:
test1 = "!f() { \
REPLACED=`echo ${1} | awk '{gsub(/^remotes\/[^\/]*\//, "")}{print}'`; \
git push -d origin $REPLACED;
}; f"
Two working git aliases from the accepted answer:
Replace echo
with the actual command you need, e.g. git push -d origin
- Using awk as originally asked for (can be useful in many situations):
v1_awk = "!f() { \
replaced=$(echo "$1" | awk '{gsub(/^remotes\\/[^\\/]*\\//, \"\")}{print}'); \
echo "$replaced"; \
}; f"
- Using shell string substitutions (for those who can remember how it works):
v2_shell = "!f() { echo \"${1#remotes/*/}\"; }; f"
CodePudding user response:
\/
is an unknown escape sequence, you have to escape \
inside "
.
test1 = "!f() { replaced=$(echo "$1" | awk '{gsub(/^remotes\\/[^\\/]*\\//, \"\")}{print}') && git push -d origin \"$replaced\"; }; f"
From man git config
: Inside double quotes, double quote " and backslash \ characters must be escaped: use \" for " and \\ for \.
As for shell: prefer to use lower case for local variable names, quote variable expansions to prevent word splitting, prefer to use $(...)
instead of backticks.
Anyway, I think just:
test1 = "!f() { git push -d origin \"${1#remotes/*/}\"; }; f"