Reputation: 16407
I can find a lot of answers to list tracking branches (here), what I would like to do is check what local branches can be safely deleted because the commits have been pushed to a remote already.
In most cases, I pushed those branches to the remote and they are not "tracking", so that part is not really helping. However in most cases, the remote branch has the same name and should point to the same commit (but not always true).
Seems like this should be fairly common thing to do?
Upvotes: 4
Views: 2196
Reputation: 16407
The way I found to do this is:
git branch -a --contains name_of_local_branch | grep -c remotes/origin
of course, origin
can be changed to the name of whatever remote.
This will output the number of remote branch that contains the local branch. If that number is different than 0, then I'm good to clean it up from my local repository.
Update, made it into a script:
#!/bin/bash
# Find (/delete) local branches with content that's already pushed
# Takes one optional argument with the name of the remote (origin by default)
# Prevent shell expansion to not list the current files when we hit the '*' on the current branch
set -f
if [ $# -eq 1 ]
then
remote="$1"
else
remote="origin"
fi
for b in `git branch`; do
# Skip that "*"
if [[ "$b" == "*" ]]
then
continue
fi
# Check if the current branch tip is also somewhere on the remote
if [ `git branch -a --contains $b | grep -c remotes/$remote` != "0" ]
then
echo "$b is safe to delete"
# git branch -D $b
fi
done
set +f
Upvotes: 4