Delete multiple remote branches in git

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

    Github also has a nice UI and mechanism for quickly deleting branches, that's if you'd rather use a UI

    0 讨论(0)
  • 2020-12-07 08:01

    Warning: This will delete all remote branches. Use with caution.

    I think my way deleting remote branches is the best.

    git branch -r | grep -v master | sed 's/origin\//:/'| xargs git push

    Warning: This will delete all remote branches. Use with caution.

    0 讨论(0)
  • 2020-12-07 08:02

    Everyone is using awk, not sure why. I feel like that's more complex. Here is what I use to delete all remote branches on my fork remote:

    $ git branch -r --list 'fork/*' | sed 's/fork\///' | xargs git push --delete fork
    

    Throw in a grep between the xargs and sed if you need to filter the list down to only a subset of remote branches.

    0 讨论(0)
  • 2020-12-07 08:04

    I tried to delete all origin/release/r1-1* remote branches, hence following command line worked nicely.

    git branch -r | awk -Forigin/ '/\/*r1-1/ {print $2}' |  xargs -I {} git push origin :{}
    
    0 讨论(0)
  • 2020-12-07 08:05

    Use the following command to remove all branches with PREFIX prefix on remote server.

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

    You may want to do a dry-run first to see if it is the branches that you want to remove:

    git branch -r | awk -F/ '/\/PREFIX/{print $2}'
    
    0 讨论(0)
  • 2020-12-07 08:05

    If you like a simpler approach, for instance delete 3 or 4 branches:

    git push origin --delete <branch1> <branch2> <branch3>
    

    Important: Only works on Git v1.7.0 and above.

    0 讨论(0)
提交回复
热议问题