Is there a way to fetch the names of refs from a remote in Git without fetching any objects associated with those refs?

StackOverflow https://stackoverflow.com/questions/22292784

  •  12-06-2023
  •  | 
  •  

Frage

I'm writing a script that acts on a local repo where the remote repo is large, slow, and has many branches with divergent histories. The typical user only cares about a small number of these branches, so I'd like to save bandwidth by not fetching objects from branches they won't use.

To do that, I'd like to be able to fetch the names of refs from the remote without fetching the actual objects associated with those refs. (Once I have the names of all those refs, I plan to present that list to the user and allow them to select which branches they want, so that I can programmatically build a very narrow refspec that only includes the branches that are interesting to them.)

Is there a way in Git to query a remote repo for all of its ref names (in this case, a few KB) without also incurring the cost of fetching all of its objects (in this case, several GB)? (FWIW, users may be using either ssh or https URLs.)

War es hilfreich?

Lösung

As far as I know, git show remote origin or git ls-remote can achieve this to some extent. You might need to do some extra filtering or matching to get what you want.

The following is shown as an example:

$ git remote show origin 
* remote origin
  Fetch URL: git@10.88.1.128:Test
  Push  URL: git@10.88.1.128:Test
  HEAD branch: master
  Remote branches:
    client                      tracked
    develop                     tracked
    index                       new (next fetch will store in remotes/origin)
    master                      tracked
    refs/remotes/origin/masster stale (use 'git remote prune' to remove)
    server                      tracked
    test                        tracked
  Local branches configured for 'git pull':
    masster merges with remote masster
    master  merges with remote master
  Local ref configured for 'git push':
    master pushes to master (local out of date)

The index branch is newly created by others and not stored in local.

EDIT: a better example would be a newly created git repository with just remote url set and nothing else.

git ls-remote:

$ git init
Initialized empty Git repository in /home/erix/gitRepos/test/.git/
$ git remote add origin git@10.88.1.128:Test
$ git ls-remote origin 
215e658c07c0e667a73ec9247f0b98c90a4fe65a        HEAD
4423ed26d1a74139997ed982cf42d681ea4eb248        refs/heads/client
1c3d2c2113d04d7771a5638729528d090d9a2eae        refs/heads/develop
8277bdd3d37b9a7c02bead816efabc6849290dc1        refs/heads/index
215e658c07c0e667a73ec9247f0b98c90a4fe65a        refs/heads/master
eb26276359ae355486536a4bfe5c939a5ab96fb0        refs/heads/server
62018d80b5d279ee2cbe8175a0bde30121288045        refs/heads/test

git remote show:

$ git remote show origin
* remote origin
  Fetch URL: git@10.88.1.128:Test
  Push  URL: git@10.88.1.128:Test
  HEAD branch: master
  Remote branches:
    client  new (next fetch will store in remotes/origin)
    develop new (next fetch will store in remotes/origin)
    index   new (next fetch will store in remotes/origin)
    master  new (next fetch will store in remotes/origin)
    server  new (next fetch will store in remotes/origin)
    test    new (next fetch will store in remotes/origin)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top