Whymarrh's comment is correct: there's no built-in command to do this.
There is a good reason there is no such built-in, as well. Just because the upstream remote-tracking branch has been pruned doesn't mean you're necessarily ready to throw out your own work. In your example, perhaps before git fetch --prune
deleted origin/Branch1
, just maybe, the commit graph picture looked like this:
...--o--o--o <-- origin/Branch1
\
o <-- Branch1
where you made one good commit on Branch1
and were ready to git push
it but got interrupted last week. Now that origin/Branch1
is gone, perhaps you'd like to copy that good commit somewhere else. But now that origin/Branch1
is gone there's no reason to keep the kink in the graph, and all Git can see is this:
...--o--o--o--o <-- Branch1
It knows that your Branch1
has a configured upstream, but it doesn't know which commit that configured upstream pointed to, back when it existed.
Nonetheless, if you're quite certain you want to remove such branches, you can do it from a short script:
#! /bin/sh
#
# rm-if-gone: remove branches that have a configured upstream
# where the configured upstream no longer exists. Run with
# -f to make it work, otherwise it just prints what it would
# remove.
force=false
case "$1" in
-f) force=true;;
esac
for branch in $(git for-each-ref --format='%(refname:short)' refs/heads); do
# find configured upstream, if any
remote=$(git config --get branch.$branch.remote) || continue
# if tracking local branch, skip
if [ "$remote" = . ]; then continue; fi
# if the upstream commit resolves, skip
ucommit=$(git rev-parse "${branch}@{u}" 2>/dev/null) && continue
# upstream is invalid - remove local branch, or print removal
$force && git branch -D $branch || echo "git branch -D $branch"
done
(the above is untested; also it won't, because you can't, delete the current branch, so you need to be sure you're on a branch you don't intend to delete).