# [Question] How to know which branch(ref) is the latest updated branch?

3 messages from 2022-08-18 to 2022-08-18. Participants: lilinchao@oschina.cn, Dan Pristupov, Junio C Hamano.
Thread: https://gitlist.dev/t/58319

## lilinchao@oschina.cn, 2022-08-18 10:04

Subject: [Question] How to know which branch(ref) is the latest updated branch?
Message-ID: <2022081818034939145210@oschina.cn>
URL: https://gitlist.dev/e/2022081818034939145210%40oschina.cn

```
In a git based workflow, there are usually many active branches.
So, is there a convenient way to quickly know which branch is the
latest updated?

```

## Dan Pristupov, 2022-08-18 14:30

Subject: Re: [Question] How to know which branch(ref) is the latest updated branch?
Message-ID: <09d5a345-af62-c411-8fb5-b489337ebc36@danil.cz>
URL: https://gitlist.dev/e/09d5a345-af62-c411-8fb5-b489337ebc36%40danil.cz
In-Reply-To: <2022081818034939145210@oschina.cn>

```
You can use `for-each-ref` for that. For example:

ˋˋˋ
git for-each-ref --count=1 --sort='-authordate' refs/heads
ˋˋˋ

https://git-scm.com/docs/git-for-each-ref#_examples has an example how 
to get 3 recent tags


```

## Junio C Hamano, 2022-08-18 17:08

Subject: Re: [Question] How to know which branch(ref) is the latest updated branch?
Message-ID: <xmqqa681qy2k.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqa681qy2k.fsf%40gitster.g
In-Reply-To: <2022081818034939145210@oschina.cn>

```
"lilinchao@oschina.cn" <lilinchao@oschina.cn> writes:

> In a git based workflow, there are usually many active branches.
> So, is there a convenient way to quickly know which branch is the
> latest updated?

These days "git branch" has "--sort" option, inherited from "for-each-ref",
so

    git branch --sort=-committerdate

lists them from the most-recently-committed [*].

HOWEVER.

There is no way to sort on the time when each branch was last
updated.  You may do

    git branch newbranch HEAD@{2.years.ago}

to create a new branch (i.e. it is the last updated branch) that
points at a commit that existed 2 years ago (hence it would be at
least 2 years old, possibly more).  If for-each-ref learns a new
placeholder %(reflogtime) that can be used to represent the
timestamp of the latest reflog entry, you should be able to sort by
the time when branch was last updated, but not until then.


[Footnote]

I have this handy alias

    [alias]
    notyet = branch --no-merged jch --no-merged seen --sort=-committerdate '??/*'

to remind me of topics that are not yet in my integration branches
while rebuilding them.

In the end result, 'seen' is supposed to include "everything I saw
and found possibly interesting", and 'jch' is supposed to be a
subset of it, but explicitly saying "show branches that are not in
either of these two" helps while rebuilding them (and I do so a few
times a day).

```
