Loosely based on Show git ahead and behind info for all branches, including remotes, here is a shell script which should allow you to have a "ahead ... behind ..." message for any two commits :
file ./ahead.sh :
#!/bin/bash
left=$1
right=$2
leftahead=`git rev-list --count $right..$left`
rightahead=`git rev-list --count $left..$right`
echo "$left (ahead $leftahead) | (behind $rightahead) $right"
usage :
$ ./ahead.sh HEAD origin/master
HEAD (ahead 7) | (behind 0) origin/master
git rev-list $left..$right
displays the sequnce of commits starting from (but not including) the common ancestor for $left
and $right
up to $right
.
git rev-list --count $left..$right
will simply count the number of commits in this sequence.
Using both git rev-list --count $left..$right
and git rev-list --count $right..$left
, you get the ahead/behind counts (what is "ahead" and "behind" depends on which commit serves as reference).
For a list of modified files (à la git status
), you can use git diff --name-status
:
#!/bin/bash
left=$1
right=$2
leftahead=`git rev-list --count $right..$left`
rightahead=`git rev-list --count $left..$right`
echo "$left (ahead $leftahead) | (behind $rightahead) $right"
git diff --name-status $right $left
update
If git diff --name-status
produces too long an output, the pager will kick in, and hide the initial ahead | behind
line. Here is a fix, using the --no-pager
option :
#!/bin/bash
function displayStatus {
left=$1
right=$2
leftahead=`git rev-list --count $right..$left`
rightahead=`git rev-list --count $left..$right`
echo "$left (ahead $leftahead) | (behind $rightahead) $right"
git --no-pager diff --name-status $right $left
}
displayStatus $1 $2 | less