Determine whether two Git branches have diverged

两盒软妹~` 提交于 2019-12-04 13:01:45

I'm using this shell script snippet for that purpose:

git_is_merged () {
    local revlist
    if revlist=$(git rev-list -1 "$1" --not "$2"); then
        if [ "$revlist" = "" ]; then
            echo "'$1' IS merged into '$2'."
        else
            echo "'$1' is NOT merged into '$2'."
        fi
    fi
}

alias gim='git_is_merged'

Use it like gim origin/devel origin/master to determine whether origin/devel is merged into origin/master.

Edit: For the sake of completeness, if you are working with named branches only, you could also use

git branch --contains origin/devel | grep -q origin/master && echo "Merged" || echo "Not merged"

or

git branch --merged origin/master | grep -q origin/devel && echo "Merged" || echo "Not merged"

for the same purpose.

You can use git merge-base

A description can be found here.

If it can simply be fast-forwarded, git merge --ff-only otherbranch will succeed. (And if it can't, it will be rejected rather than a merge commit being made.)

If you want a graphical tool, you can use gitk branch1 branch2. This also allows you to inspect the diverging commits, if needed.

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