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

4

3 回答 3

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.

于 2013-10-03T23:26:14.457 回答
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.

于 2013-10-03T16:05:02.657 回答
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);
}
于 2013-10-04T02:45:51.110 回答