What I am trying to do is ensure that I can delete local branches safely.
I found some great answers to this question here:
How can I know in git if a branch has been already merged into master?
So we have the source branch and the destination branch. The source branch is the one that may or may not be completely merged into the destination branch already.
The problem with the answers in the above link, is that the answers don't seem to work if the destination branch has new commits after being merged with the source branch.
I have this script that works well, but it only seems to work if all the branches share the same tip, or whatever. The script work in theory, though, because you are just trying to see if the tip of the local branch is included as a commit somewhere in the history of the remote branches, it shouldn't be that hard to figure out.
#!/usr/bin/env bash
green='\033[1;32m'
red='\e[31m'
no_color='\033[0m'
branch="${1:-HEAD}"
branch_name=`git rev-parse --abbrev-ref $branch`;
git fetch origin dev;
git fetch origin master;
merge_base="$(git merge-base $branch origin/dev)"
merge_source_current_commit="$(git rev-parse $branch)"
if [ "$merge_base" != "$merge_source_current_commit" ]; then
echo -e "${red}Branch with name '$branch_name' is not completely merged with origin/dev.${no_color}";
exit 1;
else
echo -e "${green}Branch with name '$branch_name' is merged with origin/dev, now checking against origin/master${no_color}";
fi
merge_base="$(git merge-base $branch origin/master)"
if [ "$merge_base" != "$merge_source_current_commit" ]; then
echo -e "${red}Branch with name '$branch_name' is not completely merged with orign/master.${no_color}";
exit 1;
fi
echo -e "${green}branch with name '$branch_name' is completely merged with origin/dev and origin/master.${no_color}"
echo "To delete this branch run: git branch -d '$branch_name'"
Does anyone know why it wouldn't work if the destination branch gets new commits after being merged with the source branch?