Delete multiple remote branches in git

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

    This may be a duplicate answer but below tested and worked for me perfectly.

    1. Delete local branch forcefully

    git branch -D branch-name

    1. Delete Remote branch

    git push origin --delete branch-name

    1. Delete more than 1 local branch

    git branch -D branch-name1 branch-name2

    1. Delete more than 1 remote branch

    git push origin --delete branch-name1 branch-name2

    1. Delete local branch with prefix. For example, feature/*

    git branch -D $(git branch --list 'feature/*')

    git branch -D backticks $(git branch --list 'feature/*' backticks)

    1. List remote branch with prefix.

    git branch -r | grep -Eo 'feature/.*'

    1. Delete remote branch with prefix

    git branch -r | grep -Eo 'feature/.*' | xargs -I {} git push origin :{}

    0 讨论(0)
  • 2020-12-07 07:53

    The same with grep: git branch -r | grep -Eo 'PREFIX/.*' | xargs -i git push origin :{}.

    branch -r shows origin/prefix/branchname. So it will take prefix/branchname.

    0 讨论(0)
  • 2020-12-07 07:53

    Thanks to Neevek. This worked well after reconfiguring it for my purpose:

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

    I also needed take the folder structure into account. My feature-branches are in a folder structure like origin/feature/PREFIX-FEATURENUMBER. So i had to build up my pattern from $2=folder + $3= branchname.

    0 讨论(0)
  • 2020-12-07 07:53

    I was not able to use awk because we are using a slash structure for our branches' name.

    git branch -r | grep "origin/users/YOURNAME" | sed -r 's/^.{9}//'| xargs -i  sh -c 'git push origin --delete {}'
    

    This get all remote branch, get only the one for a single user, remote the "origin/" string and execute a delete on each of them.

    0 讨论(0)
  • 2020-12-07 07:53

    Previous answers helped me to remove all release branches from 2018. I ran this on my windows 10 command prompt. I have installed clink, so Linux like commands works for me.

    Dry Run:

    git branch -a | grep -o "release-.*2018" | xargs -I {} echo {}
    

    If dry run shows branches that are not in remote/origin. Run below git prune command to fix and check again.

    git remote prune origin
    

    Delete once you are happy with the result above:

    git branch -a | grep -o "release-.*2018" | xargs -I {} git push origin --delete {}
    

    If you see: error: unable to delete 'release-...2018': remote ref does not exist. Then run the previous prune command and try again.

    0 讨论(0)
  • 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 {}
    
    0 讨论(0)
提交回复
热议问题