Skip to content
LatchkeyLatchkey home

git ls-remote: Usage, Options & Common CI Errors

git ls-remote shows the refs and their SHAs on a remote - no clone required.

ls-remote is the cheap way to check if a tag exists or what SHA a branch points to before doing real work.

What it does

git ls-remote queries a remote repository and prints its references (branches, tags) and the object names they point to, without downloading objects.

Common usage

Terminal
git ls-remote --heads origin
git ls-remote --tags origin
git ls-remote origin main
# fail if a tag is missing:
git ls-remote --exit-code --tags origin v1.2.0

Options

FlagWhat it does
--headsList branch refs only
--tagsList tag refs only
--exit-codeExit non-zero if no matching refs
--refsFilter out peeled/dereferenced tag entries
-q / --quietSuppress non-ref output

Common errors in CI

fatal: could not read Username for 'https://…' or 'Authentication failed' - a private remote needs credentials even just to list refs. Provide a token in the URL. An empty result with --exit-code returns 2, which scripts can use to branch on existence.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

git ls-remote: Usage, Options & Common CI Errors?
ls-remote is the cheap way to check if a tag exists or what SHA a branch points to before doing real work.
What it does?
git ls-remote queries a remote repository and prints its references (branches, tags) and the object names they point to, without downloading objects.
Common errors in CI?
fatal: could not read Username for 'https://…' or 'Authentication failed' - a private remote needs credentials even just to list refs. Provide a token in the URL. An empty result with --exit-code returns 2, which scripts can use to branch on existence.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card