简体   繁体   English

用于重命名 github 中除开发和主控之外的所有分支的 shell 脚本

[英]shell script to rename all branches in github except develop and master

I have 100 of branches in github under one repo to rename it with prefix "feature/" except develop & master branch.我在一个 repo 下的 github 中有 100 个分支,用前缀“feature/”重命名它,除了开发和主分支。

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.这涉及到很多复杂的 sed 和 awk 解析,我认为你可以避免这些。 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):如果我们取消注释最后一个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

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.注意:如果您有一个名为feature的远程分支,那么在重命名该feature分支之前,您将无法在分支名称上使用feature/前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM