This answer describes how to get the (file) difference between two branches.
I was was wonder how to know how much commits my local branch diff from remote one.
I mean, number of commits ahead, beside or behind
This answer describes how to get the (file) difference between two branches.
I was was wonder how to know how much commits my local branch diff from remote one.
I mean, number of commits ahead, beside or behind
You can use the @{u}
upstream or @{push}
gitrevisions syntax to count the number of commits:
git rev-list --count ..@{u}
git rev-list --count @{u}..
Compare two random branches A
and B
, to find how many commits on A
are new to B
:
git rev-list --count B..A
A
and B
could have one of these relationships:
A
and B
are unrelated branches;A
is up-to-date with B
;A
is ahead of B
;A
is behind B
;A
is diverged with B
.To find out which relationship it is with foo.sh
:
#!/bin/bash
A=$1
B=$2
merge_base=$(git merge-base $A $B)
if [[ "${merge_base}" = "" ]];then
echo "$A and $B are unrelated"
else
num_new_to_A=$(git rev-list --count $A..$B)
num_new_to_B=$(git rev-list --count $B..$A)
if [[ "${num_new_to_A}" -eq 0 ]] && [[ "${num_new_to_B}" -eq 0 ]];then
echo "$A is up-to-date with $B"
elif [[ "${num_new_to_A}" -eq 0 ]] && [[ "${num_new_to_B}" -gt 0 ]];then
echo "$A is ${num_new_to_B} commits ahead of $B"
elif [[ "${num_new_to_A}" -gt 0 ]] && [[ "${num_new_to_B}" -eq 0 ]];then
echo "$A is ${num_new_to_A} commits behind $B"
elif [[ "${num_new_to_A}" -gt 0 ]] && [[ "${num_new_to_B}" -gt 0 ]];then
echo "$A is diverged with $B, ${num_new_to_B} commits ahead and ${num_new_to_A} commits behind"
else
:
fi
fi
To compare master
and origin/master
:
bash foo.sh master origin/master
You may just type git status
from the Git bash to get this information. Git will compare your current local branch against its remote tracking counterpart. You will see something like this:
$ git status
# On branch master
This will be followed by a status message such as the following:
# Your branch is up-to-date with 'origin/master'
# Your branch is ahead of 'origin/master' by 1 commit.
# Your branch is behind 'origin/master' by 1 commit.
# Your branch is 1 commit ahead and 2 commits behind 'origin/master'
The first message means that your local branch is completely current with the remote tracking branch. The second and third messages mean that either your branch has an extra commit versus the remote, or vice-versa. The fourth message would be shown if both your local branch and the remote branch had new commits since the point where your branched off.