In case anyone here needs it, the solution I used was a combination of several others answers found on SO.
For the branch that DO have an upstream, the question linked in current question works perfectly.
As for the branch that LACK such upstream, I went with a variation of this answer.
Here is the modified code snippet which I used :
#! /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) || true
# 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 modification that was done occurs in the first ||
, when we get the remote. In case no remote is found, the linked answer leaves the branch AS IS. That's exactly the case we're interested in, we want to SUPPRESS thewse branch. Therefore, we output true, so the line doesn't end up in an exit code.
Be warned that this solution will suppress ANY branch on local that isn't linked to a remote (this includes branches you started but didn't push yet), so use accordingly, and don't hesitate to run first without the -f
option.
Hoping this helps someone.