I have 100 of branches in github under one repo to rename it with prefix "feature/" except develop & master branch.
a. Any uppercase in the branch name should also converted to lowercase
I have tried with below approach but it is not working with feature/ however if I do feature- it rename all branches.
#!/bin/bash
for abranch in $(git branch -a | grep -v HEAD | grep remotes | sed "s/remotes\/origin\///g"); do git checkout $abranch ; done
echo "checkout done"
for k in $(git branch| grep -Ev "\bmaster(\s|$)|\bdevelop(\s|$)|\bbugfix(\s|$)|\bhotfix(\s|$)|\brelease(\s|$)"); do
echo $k
git branch -m $k feature/$k
git branch | grep -e _ | awk '{original=$1; sub("_","-"); print original, $1}' | xargs -n 2 git branch -m
git branch | grep -e [.]|awk '{original=$1; sub("[.]","-"); print original, $1}' | xargs -n 2 git branch -m
echo "-->"
git branch
done
That involves a lot of complicated parsing with sed and awk that I think you can avoid. Based on your description, this would accomplish the same thing:
#!/bin/bash
git for-each-ref --format '%(refname)' refs/remotes/origin |
cut --complement -d/ -f1-3 |
grep -vE '^(feature/.*|HEAD|main|master|develop|bugfix|hotfix|release)$' |
while read -r branch; do
newbranch="feature/$branch"
# we can use bash parameter expansion to perform string replacement
# instead of piping things through awk
newbranch="${newbranch//_/-}"
newbranch="${newbranch//./-}"
# skip if target branch already exists
git rev-parse --verify "$newbranch" > /dev/null 2>&1 && continue
echo "$branch -> $newbranch"
git branch "$newbranch" "origin/$branch"
# This would delete the original remote branch and push the
# new feature/... branch to the remote
#
#git push origin ":$branch" "$newbranch"
done
I've tested this on a local repository and it seems to work fine:
$ git branch -a
* main
remotes/origin/HEAD -> origin/main
remotes/origin/an-example-branch
remotes/origin/branch1
remotes/origin/main
remotes/origin/nifty.ui
remotes/origin/remove_old_code
$ sh rename-branches.sh
an-example-branch -> feature/an-example-branch
branch 'feature/an-example-branch' set up to track 'origin/an-example-branch' by rebasing.
branch1 -> feature/branch1
branch 'feature/branch1' set up to track 'origin/branch1' by rebasing.
nifty.ui -> feature/nifty-ui
branch 'feature/nifty-ui' set up to track 'origin/nifty.ui' by rebasing.
remove_old_code -> feature/remove-old-code
branch 'feature/remove-old-code' set up to track 'origin/remove_old_code' by rebasing.
$ git branch -a
feature/an-example-branch
feature/branch1
feature/nifty-ui
feature/remove-old-code
* main
remotes/origin/HEAD -> origin/main
remotes/origin/an-example-branch
remotes/origin/branch1
remotes/origin/main
remotes/origin/nifty.ui
remotes/origin/remove_old_code
If we uncomment that last git
command, then the script will push the new branch names to the remote (and delete the old names):
$ sh rename-branches.sh
an-example-branch -> feature/an-example-branch
branch 'feature/an-example-branch' set up to track 'origin/an-example-branch' by rebasing.
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To /home/lars/tmp/upstream
- [deleted] an-example-branch
* [new branch] feature/an-example-branch -> feature/an-example-branch
...
$ git branch -a
feature/an-example-branch
feature/branch1
feature/nifty-ui
feature/remove-old-code
* main
remotes/origin/HEAD -> origin/main
remotes/origin/feature/an-example-branch
remotes/origin/feature/branch1
remotes/origin/feature/nifty-ui
remotes/origin/feature/remove-old-code
remotes/origin/main
Note : If you have a remote branch named feature
, then you won't be able to use the feature/
prefix on branch names until you rename the feature
branch.
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.