Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the age of a git branch

Let's say for example that I have master was merged into g1 and the branch g1 has merged into master.

git merge master -m "#3 git merge master to g1"

git merge g1 -m "#3 git merge g1 to master"

...
...

git branch -r -v

>

origin/g1            8b535b9 #3 git merge master to g1

origin/master        a335421 A comment

origin/newbranch     626a6d2 branch example

Is there any way where I can see the order in which the branches were created, timestamp or SHA when the branch had its beginning? Or in some way find which one of the branches had its origin in the other? Except for the fact that I know by name that master is my oldest.

like image 471
duwo Avatar asked Jan 23 '15 11:01

duwo


2 Answers

If you are interested when you have created the branch (in your local repository), and the branch in question was created not long time ago (less than 90 days ago with default settings), you can look in its reflog for the creation event.

$ head -1 .git/logs/refs/heads/<branch_name> 
000000... 4a28f1... J Hacker <[email protected]> \
    1400885439 +0200    branch: Created from HEAD

Here SHA-1 identifiers were shortened for better readibility, and the line was shown broken - in real output it is one long line.

The time here is in UNIX timestamp (seconds since epoch) plus numeric timezone. You can use date to turn it into human readable output, for example:

$ date --date=@1400885439 --rfc-2822 
Sat, 24 May 2014 00:50:39 +0200
$ TZ=GMT-2 date --date=@1400885439 --rfc-2822 
Sat, 24 May 2014 00:50:39 +0200

Unfortunately as of git version 2.2.1 there is no pretty format for reflog date. Below there all reflog-related pretty formats to date:

  • '%gD': reflog selector, e.g., refs/stash@{1}
  • '%gd': shortened reflog selector, e.g., stash@{1}
  • '%gn': reflog identity name
  • '%gN': reflog identity name (respecting .mailmap, see
  • '%ge': reflog identity email
  • '%gE': reflog identity email (respecting .mailmap, see
  • '%gs': reflog subject


If you are interested when somebody else have created the branch, the situation is much more difficult.

First, you can only find the commit where the branch was forked from, and its commit date; the branch could have been created later. As @Jubobs wrote in comment, Git doesn't record the date of a branch's creation, aside from the reflog (which is strictly local).

Second, you also need to know the branch (or a set of branches) that the branch in question was forked off; the repository looks exactly the same if branch foo was forked at commit A from branch bar, and when branch bar was forked at commit A from branch foo (with exception of local reflog information).

Assuming that the branch in question is a topic branch forked from one of integration branches: maint, master or next, we could use git merge-base --all to find common ancestor, then git show with appropriate format to find commit date, as in Mykola Gurov answer below. For example:

$ echo maint master next |
while read -d ' ' branch; do 
    git merge-base --all $branch <branch_name>;
done | sort | uniq |
xargs git show -s --format=format:%ci
like image 74
Jakub Narębski Avatar answered Sep 19 '22 14:09

Jakub Narębski


You could try to find the date of the first commit a branch deviated from master, although I am not sure how reliable will it be. Something along the lines:

git branch | { while read branch; do merge_base=$(git merge-base --all $branch master); date_branched=$(git show -s --format=format:%ci $merge_base); echo "$branch: $merge_base @ $date_branched"; done }
like image 26
Mykola Gurov Avatar answered Sep 20 '22 14:09

Mykola Gurov