3

The new Bitbucket Branches page is awesome. It shows how many commits each branch is ahead/behind master. Is there an Git alias that displays same information?

The information should display:

  • Branch name
  • When it was updated last
  • How many commits its behind master
  • How many commits its ahead of master

http://i.imgur.com/VsOH4cr.png

Gaui
  • 8,723
  • 16
  • 64
  • 91

3 Answers3

2

Take a look at the script I posted in an answer to a slightly different question: https://stackoverflow.com/a/18760795/683080

You need to make sure you're up-to-date with a git fetch or git fetch --all. And once you are, you can use it to display how far ahead or behind the branches are. Git doesn't store cached versions of this information, so if you're branch is really far behind (like 6,000 commits behind the current tip of the Linux kernel), then it may take a little while to compute. Otherwise, it's pretty quick.

One small note: the script doesn't show the date it was last updated, but it does show the rest.

Community
  • 1
  • 1
John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
1

If you setup 'master' as the upstream tracking branch:

git branch --set-upstream-to master

And then do git branch -vv, should see how many commits your topic branch is ahead/behind. As for the last time a branch got updated, I don't think Git stores such information.

FelipeC
  • 9,123
  • 4
  • 44
  • 38
  • `git branch -vv` doesn't show such information (at least on OSX and git 1.8.4): all I can see is *branch name, sha, upstream, last commit message* – om-nom-nom Oct 03 '13 at 16:10
  • Exactly. Also `git status` shows this, so it must be a way to get this information. – Gaui Oct 03 '13 at 16:24
1

I use a little perl script that just uses the 'git log branch1..branch2' command to figure out how many commits are in branch2 but not in branch1, and vice-versa. It runs this for every branch against a specified base.

I install this directly in the git-core folder as a file called git-brstatus. It shows:

> git brstatus master
Behind    Branch                        Ahead
3         dev                           2

That's for this branch graph:

┌─[HEAD]──[master]──6
├ 5
├ 4
│ ┌─[dev]──3
│ ├ 2
├─┘
└ 1

It should be easy to extend this to gather the last commit date for each branch. Here's the source.


#!/usr/bin/perl

if($#ARGV != 0) {
    die "\nUsage: git-brstatus \n";
}

my $base = $ARGV[0];
my @branches = split(/\n/, `git branch`);
printf "%-10s%-30s%-10s\n", 'Behind', 'Branch', 'Ahead';
foreach my $branch (@branches) {
    $branch =~ s/\s//g;
    $branch =~ s/\*//g;
    next if $branch eq $base;
    my @ahead_commits = split(/\n/, `git log --oneline $base..$branch`);
    my @behind_commits = split(/\n/, `git log --oneline $branch..$base`);
    printf "%-10d%-30s%-10d\n", ($#behind_commits+1), $branch, ($#ahead_commits+1);
}
randy-wandisco
  • 3,649
  • 16
  • 11