Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git find the head commit of all branches

Tags:

git

git-branch

is there a possibility to get all branches of an git repository with thier head commit.

If I have the following repository:

* b562239 (HEAD -> master) lastCommit
* 3828834 (seccondBranch) seccond Commit 
| * 3f6fdf6 (firstBranch) branchCommit
|/  
* b051ccd init repo

I want to get a list like that:

master b562239
seccondBranch 3828834
firstBranch 3f6fdf6
like image 503
NelDav Avatar asked Nov 02 '25 03:11

NelDav


1 Answers

The plumbing tool for refs is git for-each-ref

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

for the exact output you wanted.


Also worth noting, git branch -v (or -vv for even slightly more verbose) will list all branches with the commit hash their tip points to, but in a much verbose way, since it also features info about remote branches association / last commit message.

Example output of a branch with -v :

development    f06f99b5c4 [behind 1] <commit message of commit f06f99b5c4>

Example output of a branch with -vv :

development    f06f99b5c4 [origin/development: behind 1] <commit message of commit f06f99b5c4>
like image 159
Romain Valeri Avatar answered Nov 04 '25 04:11

Romain Valeri



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!