Head commit for all remote branches using Git

I know how to list remote branches

$ git branch -a 

And I know how to find the hash of the head of my current branch

 $ git rev-parse HEAD 

But I'm not sure how to list all the hashes to fix the chapter for all remote branches. This is close to what I want, but in what order are they located?

 $ git rev-parse --remotes 4b9f7128e9e7fa7d72652ba49c90c37d0727123d 4ebab9616fac6896b7827e8502b4dc7c5aac6b5b ea7a5fab4a757fb0826253acf1fe7d8c546c178e ... 

Ideally, I need a list of par hashes of the branch name or even a way to pass the name of the remote branch to git rev-parse HEAD

+6
git commit
source share
3 answers

Use

 git branch -r -v --no-abbrev 

and ignore the commit message part or

 git show-ref 

and filtering results starting with refs / remotes.

+9
source share

I know this is old and answered, but I think git ls-remote will work for this too.

 git ls-remote --heads origin fcce961b46784fae13be8a30c2622ddd34d970ec refs/heads/develop 9da7bb692a72235451706f24790a3f7a100a64e2 refs/heads/feature-netty-testing 86020c50d86691caecff4a55d3b1f2f588f6291d refs/heads/javafx-testing 871d715e5c072b1fbfacecc986f678214fa0b585 refs/heads/master 7ed641c96d910542edeced5fc470d63b8b4734f0 refs/heads/orphan-branch 
+5
source share

You can use git rev-parse for this. It can accept anything that looks even remotely like a commit, and returns the full SHA1 hash for that commit.

For example, to get SHA1 from HEAD :

 git rev-parse HEAD 

To get SHA1 from master :

 git rev-parse master 

To get SHA1 from origin/trunk :

 git rev-parse origin/trunk 

To get the SHA1 of all the remote heads (this is just one of many ways to do this and, of course, not the best):

  git branch -r | cut -d' ' -f 3 | while read remote; do echo ${remote} `git rev-parse ${remote}` done 
+1
source share

All Articles