Forked from radlinskii/git_branch_delete_all_but.sh
Created
February 4, 2022 13:46
-
-
Save voidp34r/8693fbdd2e3fe92dbf0f41637215ed51 to your computer and use it in GitHub Desktop.
Delete all the git branches but "master", "main", current branch, and every branch name you provide as argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| function git_branch_delete_all_but() { | |
| branch_names_to_keep=("$@") | |
| branch_names_to_keep+=("master") # do no delete master | |
| branch_names_to_keep+=("main") # do no delete main | |
| branch_names_to_keep+=$(git symbolic-ref --short -q HEAD) # do not delete current branch | |
| branch_names_to_delete=() | |
| for branch_name in $(git for-each-ref --format='%(refname:short)' refs/heads/); do | |
| if [[ ! " ${branch_names_to_keep[*]} " =~ " ${branch_name} " ]]; then | |
| branch_names_to_delete+=$branch_name | |
| fi | |
| done | |
| for branchName in "${branch_names_to_delete[@]}" | |
| do | |
| git branch -D $branchName | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment