Determine if two Git branches diverged

I would like to determine if the two Git branches diverge or if one of the branches can simply be redirected to another branch.

In other words, I want to check if the current HEAD of one of the branches has been merged with another branch at some point, or if it contains commits that are not on the other branch.

Is there a way to do this without actually merging the two branches? Simple git diff

will not help in this case.


source to share

4 answers

I am using this script snippet for this purpose:

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

alias gim='git_is_merged'


Use it like gim origin/devel origin/master

to determine if it is merged origin/devel

into origin/master


Change: . For the sake of completeness, if you are only working on the specified branches, you can also use

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



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


for the same purpose.



you can use git merge-base

The description can be found here .



If it can be simply forwarded it git merge --ff-only otherbranch

will be successful. (And if it cannot, it will be rejected rather than merged.)



If you want to use a graphical tool, you can use gitk branch1 branch2

. It also allows you to check for diverging commits if needed.



All Articles