Delete multiple remote branches in git

前端 未结 18 2483
轮回少年
轮回少年 2020-12-07 07:11

I have a team member who inadvertently pushed over 150 of his local branches to our central repo. Thankfully, they all have the same prefix. Using that prefix, is there a gi

18条回答
  •  死守一世寂寞
    2020-12-07 07:53

    Good solution in case of multiple remotes where we can find few PREFIX combinations. If you have many (let's say hundreds) branches that were created automatically for example such pattern: build/XXXX. In addition, there is upstream remote and forked origin so that branch -r returns origin/build/XXXX and upstream/build/XXXX as well.

    You can use solution with command cut -f2- -d/ More: https://unix.stackexchange.com/a/354984

    Dry run where one can combine safe regex patterns like: 33[1-3][0-9] or [0-9]{4}:

    git branch -r | grep -Eo "upstream/build/33[0-9][0-9]" | cut -f2- -d/ | xargs -I {} echo {}
    

    The same with real delete from the upstream:

    git branch -r | grep -Eo "upstream/build/33[0-9][0-9]" | cut -f2- -d/ | xargs -I {} git push upstream --delete {}
    

提交回复
热议问题