Created
January 24, 2019 11:15
-
-
Save digitalist/02418e6819a45df00bfafb69fca6409f to your computer and use it in GitHub Desktop.
Git local branches cleanup script
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/bash | |
| SAVE_BRANCHES="master|production|testing" | |
| #RU: этот скрипт удалит все локальные ветки КРОМЕ указанных в переменной SAVE_BRANCHES | |
| #RU: если запустить его БЕЗ аргументов (./git-cleanup.sh) - он спросит разрешения и выведет | |
| #RU: список удаляемых веток. C любым аргументом (./git-cleanup.sh 1) - удалит лишние ветки | |
| #EN: This script will delete all local branches EXCEPT those in SAVE_BRANCHES_VAR | |
| #EN: Running it without args will display deletion candidates and prompt you to action. | |
| #EN: Running it with any arg (./git-cleanup.sh 1) will just delete those branches | |
| # todo: declare -a SAVE_BRANCHES=$(cat .git-save-branches) | |
| function delete_branches(){ | |
| declare -a DEL_BRANCHES=`git branch | grep -E -v ".*(${SAVE_BRANCHES})$"` | |
| echo ${DEL_BRANCHES} | |
| DEL_BRANCHES_ARR=($(echo $DEL_BRANCHES | tr ' ' "\n")) | |
| for i in "${DEL_BRANCHES_ARR[@]}" | |
| do | |
| echo "> ${i}" | |
| git branch -D ${i} | |
| # or do whatever with individual element of the array | |
| done | |
| } | |
| function prompt_deletion(){ | |
| while true; do | |
| echo "These local branches will be DELETED: " | |
| echo "" | |
| echo `git branch | grep -E -v ".*(${SAVE_BRANCHES})$"` | |
| echo "" | |
| read -p "Delete? [y/n]" yn | |
| case $yn in | |
| #[Yy]* ) echo `git branch -D | grep -E -v ".*(${SAVE_BRANCHES})$"`; git branch; break;; | |
| [Yy]* ) delete_branches; break;; | |
| [Nn]* ) exit;; | |
| * ) echo "Please answer yes or no.";; | |
| esac | |
| done | |
| } | |
| if [ $# -eq 0 ] | |
| then | |
| prompt_deletion | |
| else | |
| delete_branches | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment