Created
April 10, 2020 21:02
-
-
Save robballou/9c37662d0862d8698ce61a07c4ad347d to your computer and use it in GitHub Desktop.
Find branches that are removed/deleted from remote
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/zsh | |
# | |
# Checks local branches to see if the branch is on remote and tracking remote that is still there. | |
# | |
# Does not delete branches automatically, but will provide a command in cases where you have a local | |
# branch that is not on remote and is tracking a now-gone remote branch. | |
# | |
main() { | |
current_branch=$(git branch-name) | |
git fetch &> /dev/null | |
# check if this repo has a development branch | |
# git show-ref --verify --quiet refs/heads/development | |
# has_development=$? | |
# # check if this repo has a master branch | |
# git show-ref --verify --quiet refs/heads/master | |
# has_master=$? | |
git branch --all | grep -vE "(remote|master|development)" | tr -d " *" | while read branch; do | |
has_remote_branch=$(git branch --remotes | grep $branch | wc -l) | |
if [[ $has_remote_branch -eq "0" ]]; then | |
git checkout $branch &> /dev/null | |
ref=$(git symbolic-ref -q HEAD) | |
is_tracking=$(git for-each-ref --format='%(upstream:short)' "$ref" | sed '/^\s*$/d' | wc -l) | |
if [[ $is_tracking -eq "1" ]]; then | |
echo "Remote does not exist and tracking branch is gone: git branch --delete --force $branch" | |
else | |
echo "Branch $branch does not exist on remote" | |
fi | |
fi | |
done | |
git checkout $current_branch &> /dev/null | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment