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?
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:will list just local branches
will list local branches and remote tracking refs. To list everything (branches, tags, remote refs, other more exotic things that might exist), simply
You can also supply a
format
option. For examplewill 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.
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.You can add some colors to make it clear.
Output:
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:
I've used the following paramters in the
clone
command to make it a bit quicker.If you run the script:
./summary.sh <your repo url>
It will show you the summary as you would expect!
Hope it helps :)
Just add
--no-walk
to your log command.You can use the following code to get the branch name and last commit date
The result is as below image
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:
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: