检查分支是否已合并到远程的master或dev分支中 [英] Checking if branch has already been merged into master or dev branches on remote

查看:261
本文介绍了检查分支是否已合并到远程的master或dev分支中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做的是确保我可以安全地删除本地分支.

What I am trying to do is ensure that I can delete local branches safely.

我在这里找到了一些很好的答案:

I found some great answers to this question here:

如何在git中知道分支是否已合并到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?

推荐答案

有一种更简单的方法可以使用以下方法来测试本地分支的尖端是否作为提交包含在远程分支的历史记录中": /p>

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.

这篇关于检查分支是否已合并到远程的master或dev分支中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆