Checking if branch has already been merged into master or dev branches on remote

喜你入骨 提交于 2019-12-14 02:14:34

问题


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?


回答1:


There is a simpler way to test "if the tip of the local branch is included as a commit somewhere in the history of the remote branches" by using:

if [ git merge-base --is-ancestor source destination ] ...

However I can't say that it will work better as I was unable to recreate your original problem.



来源:https://stackoverflow.com/questions/51355331/checking-if-branch-has-already-been-merged-into-master-or-dev-branches-on-remote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!