'The Problem with listing all remote branches with latest commit date in git bash

I wanted to run this command line in my git bash, but I can not see any result.

My Goal is: Listing all remote branches with latest commit date.

the command looks like this

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

my qustion is, 1: how to run sh command in git bash. 2: is here somthing wrong in this command? systex erro?

any solutions?



Solution 1:[1]

Clone a new repository for this task.

git clone <repo_url>

It's better to update all the remote tracking branches every time. But in your case, it depends.

git fetch origin -q +refs/heads/*:refs/remotes/origin/*
git for-each-ref refs/remotes/origin | grep -v 'refs/remotes/origin/HEAD' | while read s t r;do
    echo $r $(git log -1 --pretty='%ci %cr' $s)
done

It can be simpler.

git fetch origin -q +refs/heads/*:refs/remotes/origin/*
git for-each-ref refs/remotes/origin --format="%(refname) %(committerdate:iso) %(committerdate:relative)"  | grep -v 'refs/remotes/origin/HEAD'

A shell script sample. Suppose the repository is at /path/to/repo.

#!/bin/bash

if [[ ! -d /path/to/repo ]];then
    git clone --no-checkout <repo_url> -- /path/to/repo
fi

export GIT_DIR=/path/to/repo/.git
git fetch origin -q +refs/heads/*:refs/remotes/origin/*
git for-each-ref refs/remotes/origin --format="%(refname) %(committerdate:iso) %(committerdate:relative)"  | grep -v 'refs/remotes/origin/HEAD'
unset GIT_DIR

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1