Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collecting a list of all branches in a repository in a special format [Git]

Is there a way to collect a list of the branch in a repository and there last commit date?

In other words, I would like to print something like this to the stdout:

branch_name_1 date1
branch_name_2 date2
branch_name_3 date3

Is it possible?

EDIT: I tried to use the following steps:

  1. git log --pretty=format:"%ad:%an:%d:%B" --date=short --branches

  2. git branch -a

  3. git ls-remote –heads

each one of them gives me the branches of the repository which I currently in. But now I would like to check if it possible to run the command from every directory (to a specific repo). Also to print in a special format. Still trying to understand how to get the date of the last commit.

Another-Edit: I thought about it and it's the best to use git ls-remote –heads because I would like to check a non-local repo. how can I check that date for each one of the branches in the git ls-remote –heads output?

like image 668
TTaJTa4 Avatar asked Jan 27 '26 18:01

TTaJTa4


1 Answers

If your Git version is >= 2.13, you can use the same git branch command to do this.

It has a --format parameter which you can use to format the output as you wish. Just run it like this to get your desired output.

git branch --format='%(refname:short)%09%(committerdate)' --sort=-committerdate

You can add some colors to make it clear.

git branch --format='%(color:bold green) %(refname:short) %(color:white) %(committerdate)' --sort=-committerdate

Output:

Development  Fri Jun 29 10:32:43 2018 +0530  
feat-2180  Fri Jun 8 18:01:36 2018 +0530  
master  Wed May 16 17:19:21 2018 +0530

Here's a list of field names it supports - https://github.com/git/git/blob/v2.17.0/ref-filter.c#L328

=====================================================================

Edit:

As you have mentioned in your comments; I don't think you can get the commit details using the ls-remote command alone. Just had a look at the command's source here and it doesn't seem to return any other value except for the refs and commit hashes. So I don't think it's possible to do using this (correct me if I'm wrong).

https://github.com/git/git/blob/maint/builtin/ls-remote.c

If you are fine with making a temporary clone, I'd suggest to create a shell script or some sort of a script to do a clone and get summary.

Here's an example:

#!/usr/bin/env bash

REMOTE_URL=$1

git clone -q --depth 1 --bare --no-single-branch $REMOTE_URL /tmp/temp-git-clone
cd /tmp/temp-git-clone
git branch --format='%(color:bold green) %(refname:short) %(color:white) %(committerdate)' --sort=-committerdate
rm -rf /tmp/temp-git-clone

I've used the following paramters in the clone command to make it a bit quicker.

--depth 1 = will get only the last commit

--bare = create a bare repository, which means there will be no working directory and it won't copy the files.

--no-single-branch = need this to tell the clone command to get all branches

If you run the script: ./summary.sh <your repo url>

It will show you the summary as you would expect!

Hope it helps :)

like image 61
Nimeshka Srimal Avatar answered Jan 30 '26 20:01

Nimeshka Srimal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!