gitgithub

List remote branches - git branch -a vs git ls-remote --heads origin


Following post commands should produce the same output with regard to remote branches, however

git fetch
git branch -a 

shows remote branches that are not visible while executing

git ls-remote --heads origin

What is the reason of this behavior?

[clarification]

(TA216441) $ git pull
Your configuration specifies to merge with the ref 'refs/heads/TA216441'
from the remote, but no such ref was fetched.

(TA216441) $ git fetch
(TA216441) $ git branch -a
* TA216441
  TA216442
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/TA212425
  remotes/origin/TA216441
  remotes/origin/TA219346
  remotes/origin/TA220305
  remotes/origin/TA223738
  remotes/origin/master

(TA216441) $ git ls-remote --heads origin
  hash-1 refs/heads/DE18756_2
  hash-2 refs/heads/TA212425
  hash-2 refs/heads/TA219346
  hash-3 refs/heads/TA220305
  hash-4 refs/heads/master

Solution

  • Running git branch -a lists all local and tracking branches, both of which exist locally, on your machine. Here is what I got when I ran git branch -a:

    master
    branch1
    remotes/origin/HEAD -> origin/master
    remotes/origin/master
    remotes/origin/branch1
    

    However when you run git ls-remote --heads origin it lists the remote head references in your repo. For the same repo, this is what I saw:

    b9e515799... refs/heads/master
    9a7faebd1... refs/heads/branch1
    

    The repo only has the true remote branches, which is what you are actually synching with when you use things like git pull.