[英]shell script to rename all branches in github except develop and master
我在一個 repo 下的 github 中有 100 個分支,用前綴“feature/”重命名它,除了開發和主分支。
一個。 分支名稱中的任何大寫字母也應轉換為小寫字母
我已經嘗試過以下方法,但它不適用於功能/但是如果我使用功能 - 它會重命名所有分支。
#!/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
這涉及到很多復雜的 sed 和 awk 解析,我認為你可以避免這些。 根據您的描述,這將完成同樣的事情:
#!/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
我已經在本地存儲庫上對此進行了測試,它似乎工作正常:
$ 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
如果我們取消注釋最后一個git
命令,那么腳本會將新分支名稱推送到遠程(並刪除舊名稱):
$ 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
注意:如果您有一個名為feature
的遠程分支,那么在重命名該feature
分支之前,您將無法在分支名稱上使用feature/
前綴。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.