Here we have a git repo which has multiple branches which start with the same prefix just like this:
pfx.branchName1
pfx.branchName2
pfx.branchName3
...
So the question is how to quickly remove all the prefixes ("pfx.") from all the branches and get something like this:
branchName1
branchName2
branchName3
...
Found an universal command that searches for the branches which contain our desired string (eg "StringToFind"
) and renames by replacing that part with the string we want (eg "ReplaceWith"
):
git branch | grep "StringToFind" | awk '{original=$1; sub("StringToFind","ReplaceWith"); print original, $1}' | xargs -n 2 git branch -m
Note: Before starting renaming we can run this command to see which branches are going to be renamed (just for convenience):
git branch | grep "StringToFind" | awk '{original=$1; sub("StringToFind","ReplaceWith"); print original, "->" , $1}'
So, for our case , use this for removing prefix:
git branch | grep "pfx." | awk '{original=$1; sub("pfx.",""); print original, $1}' | xargs -n 2 git branch -m
And this, for checking before removing:
git branch | grep "pfx." | awk '{original=$1; sub("pfx.",""); print original, "->", $1}'
You can filter the branch names from the output of git branch
, and then use a Bash loop with substitution to perform the renames:
git branch | sed -e 's/..//' | grep '^pfx\.' | while read b; do git branch -m "$b" "${b#pfx.}"; done
Or slightly more compactly but perhaps harder to read:
git branch | sed -ne 's/^..pfx\.//p' | while read b; do git branch -m "pfx.$b" "$b"; done
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.