问题
Is it possible to get the tags / references of a repository (eg GitHub) without downloading objects / files?
My use case is in packaging the latest beta release of some software which has a long history and is therefore large to clone.
Ideally after I determine the tag that I wish to use, I can then:
git clone -b "$tag" --depth=1
回答1:
Use git ls-remote:
$ git ls-remote -t --refs <URL>
This gives output such as:
8f235769a2853c415f811b19cd5effc47cc89433 refs/tags/continuous
24e666ed73486a2ac65f09a1479e91e6ae4a1bbe refs/tags/continuous-develop
7c2cff2c26c1c2ad4b4023a975cd2365751ec97d refs/tags/v2.0
35b69eed46e5b163927c78497983355ff6a5dc6b refs/tags/v2.0-beta10
You probably also want to pass --exit-code
to ensure a non-0
exit when no matching refs are returned.
To get only the tag names, pass through:
sed -E 's/^[[:xdigit:]]+[[:space:]]+refs\/tags\/(.+)/\1/g'
:
$ git ls-remote -t --exit-code --refs https://github.com/robert7/nixnote2.git \
| sed -E 's/^[[:xdigit:]]+[[:space:]]+refs\/tags\/(.+)/\1/g'
continuous
continuous-develop
v2.0
v2.0-beta10
Suggestions:
- Pass
--exit-code
to ensure a non-0
exit when no matching refs are returned. - Use the
https://
version: it's faster and if you're packaging you don't want to run the risk of being asked for a ssh key. --sort=-v:refname
to sort by version rather than lexographically, and have the largest versions at the top- Use
git -c versionsort.suffix=-
to prevent2.0-rc
coming "after"2.0
- Add a pattern at the end of the command line to filter. Eg
'v*'
if all version tags start with av
.
来源:https://stackoverflow.com/questions/52661928/get-only-tags-references-of-a-git-remote-repository