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
This may be a duplicate answer but below tested and worked for me perfectly.
git branch -D branch-name
git push origin --delete branch-name
git branch -D branch-name1 branch-name2
git push origin --delete branch-name1 branch-name2
git branch -D $(git branch --list 'feature/*')
git branch -D backticks $(git branch --list 'feature/*' backticks)
git branch -r | grep -Eo 'feature/.*'
git branch -r | grep -Eo 'feature/.*' | xargs -I {} git push origin :{}
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
.
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.
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.
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.
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 {}