threads / discuss / 39335

Show all branches and ahead/behind states

Subject: Show all branches and ahead/behind states

## tl;dr

8 messages between May 14, 2015 and May 15, 2015.

replies: 7people: 3as markdown or json

Robert Dailey· May 14, 2015, 14:57 UTC · lore

Is there a script or built-in functionality in git 2.4.1 that will allow me to list all local branches with the ahead and behind status of each? Basically the first line of:

$ git status -sb
But for every branch would be roughly what I'm looking for. Any ideas? Thanks.
Dennis Kaarsemaker· May 14, 2015, 15:55 UTC · re: Robert Dailey · lore

Re: Show all branches and ahead/behind states

On do, 2015-05-14 at 09:57 -0500, Robert Dailey wrote:
Show 7 quoted lines
> Is there a script or built-in functionality in git 2.4.1 that will
> allow me to list all local branches with the ahead and behind status
> of each? Basically the first line of:
> 
> $ git status -sb
> 
> But for every branch would be roughly what I'm looking for. Any ideas? Thanks.
git branch -v (or -vv) is what you're after.
-- 
Dennis Kaarsemaker
www.kaarsemaker.net
Robert Dailey· May 14, 2015, 17:18 UTC · re: Dennis Kaarsemaker · lore

Re: Show all branches and ahead/behind states

On Thu, May 14, 2015 at 10:55 AM, Dennis Kaarsemaker <dennis@kaarsemaker.net> wrote:

Show 14 quoted lines
> On do, 2015-05-14 at 09:57 -0500, Robert Dailey wrote:
>> Is there a script or built-in functionality in git 2.4.1 that will
>> allow me to list all local branches with the ahead and behind status
>> of each? Basically the first line of:
>>
>> $ git status -sb
>>
>> But for every branch would be roughly what I'm looking for. Any ideas? Thanks.
>
> git branch -v (or -vv) is what you're after.
> --
> Dennis Kaarsemaker
> www.kaarsemaker.net
>

Nice, I never noticed that -v showed ahead/behind status. Would be nice if branch had a --format option to customize the coloring of the "ahead" / "behind" text so that there was more contrast. It's a lot of information to consume especially with a lot of local branches; coloring would bring some sanity.

Thanks!
Dennis Kaarsemaker· May 14, 2015, 17:29 UTC · re: Robert Dailey · lore

Re: Show all branches and ahead/behind states

On do, 2015-05-14 at 12:18 -0500, Robert Dailey wrote:
Show 22 quoted lines
> On Thu, May 14, 2015 at 10:55 AM, Dennis Kaarsemaker
> <dennis@kaarsemaker.net> wrote:
> > On do, 2015-05-14 at 09:57 -0500, Robert Dailey wrote:
> >> Is there a script or built-in functionality in git 2.4.1 that will
> >> allow me to list all local branches with the ahead and behind status
> >> of each? Basically the first line of:
> >>
> >> $ git status -sb
> >>
> >> But for every branch would be roughly what I'm looking for. Any ideas? Thanks.
> >
> > git branch -v (or -vv) is what you're after.
> > --
> > Dennis Kaarsemaker
> > www.kaarsemaker.net
> >
> 
> Nice, I never noticed that -v showed ahead/behind status. Would be
> nice if branch had a --format option to customize the coloring of the
> "ahead" / "behind" text so that there was more contrast. It's a lot of
> information to consume especially with a lot of local branches;
> coloring would bring some sanity.

Fortunately for you, git for-each-ref lets you do just this. For example, this gives you what branch -vv does, minus coloration and padding:

git for-each-ref --format '%(HEAD) %(refname:short) %(upstream:short) %(upstream:track) %(subject)' refs/heads
-- 
Dennis Kaarsemaker
www.kaarsemaker.net
Robert Dailey· May 14, 2015, 18:13 UTC · re: Dennis Kaarsemaker · lore

Re: Show all branches and ahead/behind states

On Thu, May 14, 2015 at 12:29 PM, Dennis Kaarsemaker <dennis@kaarsemaker.net> wrote:

Show 29 quoted lines
> On do, 2015-05-14 at 12:18 -0500, Robert Dailey wrote:
>> On Thu, May 14, 2015 at 10:55 AM, Dennis Kaarsemaker
>> <dennis@kaarsemaker.net> wrote:
>> > On do, 2015-05-14 at 09:57 -0500, Robert Dailey wrote:
>> >> Is there a script or built-in functionality in git 2.4.1 that will
>> >> allow me to list all local branches with the ahead and behind status
>> >> of each? Basically the first line of:
>> >>
>> >> $ git status -sb
>> >>
>> >> But for every branch would be roughly what I'm looking for. Any ideas? Thanks.
>> >
>> > git branch -v (or -vv) is what you're after.
>> > --
>> > Dennis Kaarsemaker
>> > www.kaarsemaker.net
>> >
>>
>> Nice, I never noticed that -v showed ahead/behind status. Would be
>> nice if branch had a --format option to customize the coloring of the
>> "ahead" / "behind" text so that there was more contrast. It's a lot of
>> information to consume especially with a lot of local branches;
>> coloring would bring some sanity.
>
> Fortunately for you, git for-each-ref lets you do just this. For
> example, this gives you what branch -vv does, minus coloration and
> padding:
>
> git for-each-ref --format '%(HEAD) %(refname:short) %(upstream:short) %(upstream:track) %(subject)' refs/heads
Great idea. For some reason I can't apply coloring though:

for-each-ref --format '%(HEAD) %(refname:short) %C(bold blue)%(upstream:track)%C(reset)' refs/heads

Output:
hotfix/4.2.3.1 %C(bold blue)[ahead 1, behind 1]%C(reset)
Jeff King· May 14, 2015, 22:38 UTC · re: Robert Dailey · lore

Re: Show all branches and ahead/behind states

On Thu, May 14, 2015 at 01:13:15PM -0500, Robert Dailey wrote:
Show 10 quoted lines
> > git for-each-ref --format '%(HEAD) %(refname:short) %(upstream:short) %(upstream:track) %(subject)' refs/heads
> 
> Great idea. For some reason I can't apply coloring though:
> 
> for-each-ref --format '%(HEAD) %(refname:short) %C(bold
> blue)%(upstream:track)%C(reset)' refs/heads
> 
> Output:
> 
> hotfix/4.2.3.1 %C(bold blue)[ahead 1, behind 1]%C(reset)

It's spelled "%C(bold blue)" in the --pretty formats, but in for-each-ref, it's "%(color:bold blue)". Unifying these is one of those things that has been on the radar for a long time, but nobody has quite gotten around to it.

-Peff
Robert Dailey· May 15, 2015, 14:25 UTC · re: Jeff King · lore

Re: Show all branches and ahead/behind states

On Thu, May 14, 2015 at 5:38 PM, Jeff King <peff@peff.net> wrote:
Show 19 quoted lines
> On Thu, May 14, 2015 at 01:13:15PM -0500, Robert Dailey wrote:
>
>> > git for-each-ref --format '%(HEAD) %(refname:short) %(upstream:short) %(upstream:track) %(subject)' refs/heads
>>
>> Great idea. For some reason I can't apply coloring though:
>>
>> for-each-ref --format '%(HEAD) %(refname:short) %C(bold
>> blue)%(upstream:track)%C(reset)' refs/heads
>>
>> Output:
>>
>> hotfix/4.2.3.1 %C(bold blue)[ahead 1, behind 1]%C(reset)
>
> It's spelled "%C(bold blue)" in the --pretty formats, but in
> for-each-ref, it's "%(color:bold blue)". Unifying these is one of those
> things that has been on the radar for a long time, but nobody has quite
> gotten around to it.
>
> -Peff

Thanks Jeff! That definitely fixed it. I was not able to find a reference for this in the git docs, maybe I just overlooked it somewhere.

As a second query, is there a way to do column alignment? For example, I would like the ahead/behind status in one column, and the branch names left aligned in a 2nd column. I can't simply use tabs here since the length of column 1 may differ more greatly than a single tab character.

I imagine there is no built in way to do this, but just curious. I'd probably end up having to write some complicated script :-(

Thanks again you've been super helpful.
Jeff King· May 15, 2015, 16:59 UTC · re: Robert Dailey · lore

Re: Show all branches and ahead/behind states

On Fri, May 15, 2015 at 09:25:22AM -0500, Robert Dailey wrote:
Show 8 quoted lines
> > It's spelled "%C(bold blue)" in the --pretty formats, but in
> > for-each-ref, it's "%(color:bold blue)". Unifying these is one of those
> > things that has been on the radar for a long time, but nobody has quite
> > gotten around to it.
> >
> Thanks Jeff! That definitely fixed it. I was not able to find a
> reference for this in the git docs, maybe I just overlooked it
> somewhere.
It's in "git help for-each-ref" (see the section on "FIELD NAMES").
Show 5 quoted lines
> As a second query, is there a way to do column alignment? For example,
> I would like the ahead/behind status in one column, and the branch
> names left aligned in a 2nd column. I can't simply use tabs here since
> the length of column 1 may differ more greatly than a single tab
> character.

The "--pretty" format does have "%<" for doing printf-style alignment (see the "PRETTY FORMATS" section of "git help log"). But "for-each-ref" doesn't have any similar facility, as far as I know.

Again, this is something that it would be nice to fix.
-Peff

← back to recent threads