Delete multiple remote branches in git

前端 未结 18 2505
轮回少年
轮回少年 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:52

    Thanks to Neevek for great and elegant solution!

    But i have some troubles with slashes in branch names (i'm using Git Flow), because of awk field separator / (-F option)

    So my solution is based on Neevek's, but correctly parses branch names with /. In this case i presume that your remote called origin. Command for deleting remote branches with names staring with PATTERN:

    git branch -r | awk -Forigin/ '/\/PATTERN/ {print $2}' | xargs -I {} git push origin :{}
    

    And don't forget to check what you are going to delete:

    git branch -r | awk -Forigin/ '/\/PATTERN/ {print $2}'
    

    USEFUL TIP: If your branch names (without origin/ prefix) stored in a text file (one branch name per line), just run:

    cat your_file.txt | xargs -I {} git push origin :{}
    

提交回复
热议问题