Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get git to display the most recent n commits from all local branches

I would like to see the most recent n commits (generally 1 or 2) for all local branches in my respository.

I have tried "git log -1 --all" and "git log -1 --branches" but this doesn't have the effect I expect in git 1.8.4.

Basically I would like the equivalent of

for i in $(ls .git/refs/heads/); do echo ====$i====; git log -1 $i; done
like image 899
Spacemoose Avatar asked Oct 24 '25 17:10

Spacemoose


1 Answers

Edit, Aug 2018: here's a quick short-cut in case you only want to look at one commit from each branch tip:

git log --no-walk --branches

The way this works is that --no-walk prevents git log from looking at any commits that are not named on the command line, while --branches names, on the command line, every branch-tip commit.

(The --branches, --tags, and --remotes options all take optional glob patterns as well, so you can look at all m* commits with git log --no-walk --branches='m*' for instance.)


Local (i.e., "not remote") branches are those whose reference-name starts with refs/heads/.

The git for-each-ref command is designed to iterate over reference-names, so it's usually the thing to use:

git for-each-ref --format='%(refname:short)' refs/heads

will print them all out. Note that you can't just pass this to git log -1 though, as that will stop after logging one commit from the first reference; so you need something like what you did. There are a bunch of ways to construct this, e.g.:

git for-each-ref --format '%(refname:short)' refs/heads | \
    while read branch; do git log -1 $branch; done

or:

git for-each-ref --format='git log -1 %(refname:short)' refs/heads | sh

In the second case you should add --shell to make sure that all expansions are quoted. This protects against, e.g., a branch named foo$bar.

like image 120
torek Avatar answered Oct 26 '25 07:10

torek