Skip to content

ls-remote Git Command Guide

The git ls-remote command displays references available in a remote repository along with their associated commit IDs. It enables inspection of remote repository state without local cloning or fetching.

Terminal window
git ls-remote [--branches] [--tags] [--refs] [--upload-pack=<exec>]
[-q | --quiet] [--exit-code] [--get-url] [--sort=<key>]
[--symref] [<repository> [<patterns>...]]
OptionDescription
-b, --branchesLimit to branch references only
-t, --tagsLimit to tag references only
--refsDo not show peeled tags or pseudorefs
--upload-pack=<exec>Specify git-upload-pack path on remote
OptionDescription
-q, --quietDo not print remote URL to stderr
--exit-codeExit with code based on reference existence
--get-urlShow only remote URL
--sort=<key>Sort output by key (version:refname)
--symrefShow underlying ref for symbolic refs
ParameterDescription
<repository>Remote repository URL or name
<patterns>...Pattern to match reference names
Terminal window
# Show all references in remote repository
git ls-remote https://github.com/user/repo.git
# List references from configured remote
git ls-remote origin
# Show only branch references
git ls-remote --branches origin
Terminal window
# Show only tags
git ls-remote --tags origin
# Show both branches and tags
git ls-remote --branches --tags origin
# Show specific reference pattern
git ls-remote origin "refs/heads/feature/*"
Terminal window
# Check remote HEAD reference
git ls-remote origin HEAD
# Get remote URL only
git ls-remote --get-url origin
# Check if specific branch exists
git ls-remote --exit-code origin refs/heads/main && echo "Branch exists"
Terminal window
# Sort by version number
git ls-remote --sort=version:refname origin
# Show symbolic refs
git ls-remote --symref origin
# Pattern matching for releases
git ls-remote origin "refs/tags/v*"
#!/bin/bash
# Check if remote branch exists before merging
if git ls-remote --exit-code --heads origin "$BRANCH_NAME"; then
echo "Branch $BRANCH_NAME exists on remote"
git fetch origin "$BRANCH_NAME"
git merge "origin/$BRANCH_NAME"
else
echo "Branch $BRANCH_NAME not found on remote"
exit 1
fi
# Monitor remote repository changes
#!/bin/bash
REMOTE_URL="https://github.com/user/repo.git"
# Get current remote HEAD
current_head=$(git ls-remote "$REMOTE_URL" HEAD | cut -f1)
# Compare with previous state
if [ "$current_head" != "$(cat .last_remote_head 2>/dev/null)" ]; then
echo "Remote repository updated: $current_head"
echo "$current_head" > .last_remote_head
# Trigger update actions
./update-dependencies.sh
fi
Terminal window
# Check reference availability across remotes
for remote in origin upstream; do
echo "=== $remote ==="
git ls-remote "$remote" --heads | head -5
done
# Verify specific tag exists on all remotes
tag="v1.0.0"
for remote in origin upstream; do
if git ls-remote --exit-code "$remote" "refs/tags/$tag"; then
echo "✓ Tag $tag exists on $remote"
else
echo "✗ Tag $tag missing on $remote"
fi
done
Terminal window
# Configure remote URL with authentication
git remote set-url origin https://user:token@github.com/user/repo.git
# Use SSH for authentication
git ls-remote git@github.com:user/repo.git
# Configure upload-pack path for SSH
git ls-remote --upload-pack=/usr/bin/git-upload-pack ssh://server/repo.git
Terminal window
# Use proxy for remote access
git config http.proxy http://proxy.company.com:8080
git ls-remote https://github.com/user/repo.git
# Configure SSL settings
git config http.sslVerify true
git ls-remote https://secure-git-server.com/repo.git
Terminal window
# Limit output for large repos
git ls-remote origin | head -20
# Pattern filtering for efficiency
git ls-remote origin "refs/heads/release/*"
# Check specific references only
git ls-remote origin HEAD main develop
Terminal window
# Check multiple repositories
repos=(
"https://github.com/user/project1.git"
"https://github.com/user/project2.git"
"https://gitlab.com/team/project3.git"
)
for repo in "${repos[@]}"; do
echo "=== $(basename "$repo" .git) ==="
git ls-remote "$repo" HEAD | cut -f1
done
Terminal window
# Test remote access
git ls-remote https://github.com/user/repo.git
# Check remote configuration
git remote -v
# Verify credentials
git config credential.helper
Terminal window
# Test network connectivity
curl -I https://github.com/user/repo.git/info/refs
# Check firewall/proxy settings
git config --list | grep proxy
# Debug SSL issues
GIT_CURL_VERBOSE=1 git ls-remote https://secure-server.com/repo.git 2>&1 | head -20
Terminal window
# Check if repository exists
git ls-remote https://github.com/user/nonexistent.git 2>&1 | head -5
# Verify remote URL format
git remote set-url origin https://github.com/user/repo.git
git ls-remote origin
Terminal window
# Time remote operations
time git ls-remote origin
# Check for large reference lists
git ls-remote origin | wc -l
# Use specific patterns to limit output
git ls-remote origin "refs/heads/main"
#!/bin/bash
# Verify deployment target exists
DEPLOY_BRANCH="main"
REMOTE_URL="https://github.com/company/app.git"
if git ls-remote --exit-code "$REMOTE_URL" "refs/heads/$DEPLOY_BRANCH"; then
latest_commit=$(git ls-remote "$REMOTE_URL" "refs/heads/$DEPLOY_BRANCH" | cut -f1)
echo "Deploying commit: $latest_commit"
# Trigger deployment
./deploy.sh "$latest_commit"
else
echo "ERROR: Branch $DEPLOY_BRANCH not found on remote"
exit 1
fi
Terminal window
# Check release tag availability
release_tag="v2.1.0"
remote_url="https://github.com/company/product.git"
if git ls-remote --exit-code "$remote_url" "refs/tags/$release_tag"; then
echo "✓ Release tag $release_tag exists"
# Get release commit
release_commit=$(git ls-remote "$remote_url" "refs/tags/$release_tag^{}" | cut -f1)
echo "Release commit: $release_commit"
# Generate release notes
git log --oneline "$release_commit" ^"$(git ls-remote "$remote_url" "refs/tags/v2.0.0^{}" | cut -f1)"
else
echo "✗ Release tag $release_tag not found"
exit 1
fi
#!/bin/bash
# Monitor repository health
monitor_repo() {
local repo_url="$1"
local repo_name=$(basename "$repo_url" .git)
echo "=== Monitoring $repo_name ==="
# Check if repository is accessible
if git ls-remote --exit-code "$repo_url" HEAD; then
echo "✓ Repository accessible"
# Get latest commit
latest=$(git ls-remote "$repo_url" HEAD | cut -f1)
echo "Latest commit: ${latest:0:8}"
# Count branches
branch_count=$(git ls-remote "$repo_url" --heads | wc -l)
echo "Active branches: $branch_count"
# Count tags
tag_count=$(git ls-remote "$repo_url" --tags | wc -l)
echo "Release tags: $tag_count"
else
echo "✗ Repository not accessible"
fi
echo
}
# Monitor multiple repositories
monitor_repo "https://github.com/company/backend.git"
monitor_repo "https://github.com/company/frontend.git"
monitor_repo "https://github.com/company/docs.git"

How does ls-remote differ from git remote?

Section titled “How does ls-remote differ from git remote?”

git remote manages local remote configurations; git ls-remote queries actual remote repository state. ls-remote shows current remote references without local caching.

Can ls-remote work with private repositories?

Section titled “Can ls-remote work with private repositories?”

Yes, supports authentication via URL credentials, SSH keys, or credential helpers. Private repositories require proper authentication configuration.

What’s the performance impact of ls-remote?

Section titled “What’s the performance impact of ls-remote?”

Lightweight operation - only downloads reference information, not repository content. Network latency main performance factor for large reference lists.

How do I handle authentication for ls-remote?

Section titled “How do I handle authentication for ls-remote?”

Use URL-embedded credentials, SSH keys, or configure credential helpers. Same authentication methods as git fetch/clone operations.

By default shows peeled tags (actual commit objects). Use —refs to show only non-peeled references and pseudorefs like HEAD.

Shows underlying reference for symbolic refs. For HEAD, shows which branch it points to: HEAD -> refs/heads/main.

How do I check if a specific commit exists on remote?

Section titled “How do I check if a specific commit exists on remote?”

Use git ls-remote remote refs/heads/branch or git ls-remote remote commit-hash. Exit code indicates existence.

Can ls-remote work with Git protocols other than HTTP?

Section titled “Can ls-remote work with Git protocols other than HTTP?”

Yes, supports HTTP/HTTPS, SSH, and Git protocols. Protocol determined by repository URL format.

What’s the difference between —branches and —tags?

Section titled “What’s the difference between —branches and —tags?”

—branches shows refs/heads/, —tags shows refs/tags/. Combined use shows both. —refs excludes peeled tags and pseudorefs.

How do I use ls-remote in CI/CD pipelines?

Section titled “How do I use ls-remote in CI/CD pipelines?”

Perfect for checking remote state before operations. Use —exit-code for conditional logic. Lightweight alternative to git fetch for state checking.

Can ls-remote show remote repository size?

Section titled “Can ls-remote show remote repository size?”

No, only shows references and commit IDs. Use git clone —dry-run or repository statistics APIs for size information.

How do I handle rate limiting with ls-remote?

Section titled “How do I handle rate limiting with ls-remote?”

Implement retry logic with exponential backoff. Cache results when possible. Use conditional requests based on last modification time.

What’s the relationship between ls-remote and git fetch?

Section titled “What’s the relationship between ls-remote and git fetch?”

git fetch downloads objects and updates local references; git ls-remote only queries remote references without local changes.

Can ls-remote work with shallow repositories?

Section titled “Can ls-remote work with shallow repositories?”

Yes, works with any repository structure. Shows available references regardless of local repository depth or configuration.

How do I troubleshoot “repository not found” errors?

Section titled “How do I troubleshoot “repository not found” errors?”

Verify repository URL, check authentication, ensure repository exists and is accessible, confirm network connectivity.

Can ls-remote show remote repository configuration?

Section titled “Can ls-remote show remote repository configuration?”

No, only shows references. Use git remote or repository web interface for configuration information.

Sorts output by version number or reference name. Useful for finding latest releases or organizing branch/tag lists.

How do I use ls-remote with Git LFS repositories?

Section titled “How do I use ls-remote with Git LFS repositories?”

Works normally - LFS doesn’t affect reference listing. Shows same references as non-LFS repositories.

No, requires network access to query remote repository. Local reference information available through git show-ref or git for-each-ref.

Follows HTTP redirects automatically. For complex redirect scenarios, configure git http.followRedirects or use curl for debugging.

Zero for success, non-zero for errors. —exit-code makes exit code depend on reference existence - useful for scripting conditional logic.

  1. Remote Repository Inspection: Check remote state without local cloning or fetching
  2. CI/CD Pipeline Integration: Verify remote branch/tag existence before deployment operations
  3. Release Management: Validate release tags and branch availability across remotes
  4. Repository Monitoring: Track remote repository changes and reference updates
  5. Authentication Testing: Verify remote repository access and authentication configuration
  6. Multi-Repository Management: Batch operations across multiple remote repositories