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:
git log --pretty=format:"%ad:%an:%d:%B" --date=short --branches
git branch -a
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?
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 :)
The best starting point if you need to process a list of refs is git for-each-ref
. If you only want to process certain refs, you can supply patterns describing what you want. For example:
git for-each-ref refs/heads
will list just local branches
git for-each-ref refs/heads refs/remotes
will list local branches and remote tracking refs. To list everything (branches, tags, remote refs, other more exotic things that might exist), simply
git for-each-ref
You can also supply a format
option. For example
git for-each-ref --format='%(refname)%09%(committerdate)` refs/heads
will show each local branch with the commit date of the corresponding commit. (You could also use %(authordate)
, which might differ e.g. after a history rewrite in which commit dates would typically be changed but author dates left alone.)
A bit of an aside, but the above might look a bit off, in that you asked about the "last commit date". The oddity is that to git, a branch is just a pointer to one commit - and that commit's date is typically the last commit date for the branch, and intuitively it "should" be, and it's the closest thing that's "real" in git to the last commit date for the branch - though if the branch has been manipulated in various ways, it could be something different.
Just add --no-walk
to your log command.
git log --no-walk --branches --date=short --format="%ad:%d"
You can show the remote branch names and related dates in any of a directory (but must inside a git repo since git commands need to be executed inside git repo).
Detail steps as below:
1. Check if the directory you want to execute git commands inside a git repo
Use below command to check if the ditectory in a git repo or not:
git rev-parse --show-toplevel
2. Create a git repo if the directory not in a git repo
After checking by step1, if the directory not in a git repo, then create a git repo by git init
.
Else, skip this step.
3. Check the remote branches and date as the format you need
Use below commands to get the remote branches and date:
git remote add myremote <remote repo URL> -f
for branch in `git branch -r | grep myremote | grep -v HEAD`;do echo -e $branch \\t\\t`git show --format="%ci" $branch | head -n 1`; done | sort -r
You can use the following code to get the branch name and last commit date
for branch in git branch -r | grep -v HEAD
;do echo -e git show
--format="%ci %cr" $branch | head -n 1
\t$branch; done | sort -r
The result is as below image