Skip to content

Instantly share code, notes, and snippets.

@nordinrahman
Created February 22, 2022 08:05
Show Gist options
  • Save nordinrahman/83cfd5c7228e5bc4792a4fdf61d2f4d6 to your computer and use it in GitHub Desktop.
Save nordinrahman/83cfd5c7228e5bc4792a4fdf61d2f4d6 to your computer and use it in GitHub Desktop.
Nordin's Bash Scripting Cook Book
# git - list local branches except current branch and main/develop
# First, make sure we are on main/develop branch first
# This is to get all local branches excep current branch and main/develop without indentation
git br | grep -E "^ " | grep -Ev "^\\*" | sed s/^\ \ //g | grep -Ev "^(main|develop)$"
# delete all local branches as originally listed above
for BRANCH_NAME in $(git br | grep -E "^ " | grep -Ev "^\\*" | sed s/^\ \ //g | grep -Ev "^(main|develop)$")
do
git branch -d $BRANCH_NAME
done
# alternative syntax:
while IFS= read -r BRANCH_NAME
do
git branch -d $BRANCH_NAME
done <<< "$(git br | grep -E "^ " | grep -Ev "^\\*" | sed s/^\ \ //g | grep -Ev "^(main|develop)$")"
while IFS= read -r BRANCH_NAME
do
git branch -d $BRANCH_NAME
done <<< "$(git br | grep -E "^ " | grep -Ev "^\\*" | sed s/^\ \ //g | grep -Ev "^(main|develop)$")"
# git alias to quickly delete all merged local branches
brdel=!while IFS= read -r BRANCH_NAME; do git branch -d $BRANCH_NAME; done <<< "$(git br | grep -E "^ " | grep -Ev "^\\*" | sed s/^\ \ //g | grep -Ev "^(main|develop)$")"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment