7
git ls-remote --tags git://github.com/git/git.git

lists the latest tags without cloning. I need a way to be able to clone from the latest tag directly

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • Possible duplicate of http://stackoverflow.com/questions/791959/download-a-specific-tag-with-git – Rosa Apr 21 '15 at 18:50

3 Answers3

10

Call this ~/bin/git-clone-latest-tag:

#!/bin/bash

set -euo pipefail
basename=${0##*/}

if [[ $# -lt 1 ]]; then
    printf '%s: Clone the latest tag on remote.\n' "$basename" >&2
    printf 'Usage: %s [other args] <remote>\n' "$basename" >&2
    exit 1
fi

remote=${*: -1} # Get last argument

echo "Getting list of tags from: $remote"

tag=$(git ls-remote --tags --exit-code --refs "$remote" \
  | sed -E 's/^[[:xdigit:]]+[[:space:]]+refs\/tags\/(.+)/\1/g' | tail -n1)

echo "Selected tag: $tag"

# Clone as shallowly as possible. Remote is the last argument.
git clone --branch "$tag" --depth 1 --shallow-submodules --recurse-submodules "$@"

Then you can do:

% git clone-latest-tag https://github.com/python/cpython.git
Getting list of tags from: https://github.com/python/cpython.git
Selected tag: v3.8.0b1
Cloning into 'cpython'...
remote: Enumerating objects: 4346, done.
...
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
  • 1
    I suppose the command should be "git-clone-latest-tag" and not "git clone-latest-tag", isn't it? – Cris70 Jun 11 '20 at 07:38
  • 2
    @Cris70 not so, `git` internally looks for scripts called `git-*` Using `git clone-latest-tXX` will also give you `The most similar command is clone-latest-tag ` – Tom Hale Jun 27 '20 at 05:02
  • For Windows users: Open git-bash. Type `cd /bin`. Then type `start .` to open `/bin` directory in your explorer (usually `C:\Program Files\Git\usr\bin`). Insert the script here. – Mir-Ismaili Apr 12 '21 at 14:24
2

It's an old question, but not answered to my satisfaction (i.e. a readable, mostly-default one-liner :-) ).

This one-liner will get you a clone of a repo of just the latest tag.

REPO=https://github.com/namespace/repo.git && \
git clone $REPO --single-branch --branch \
$(git ls-remote --tags --refs $REPO | tail -n1 | cut -d/ -f3)

Explanation

  1. First we set a REPO variable. We need it twice, this reduces the chance of errors.
  2. Then we use $(git ls-remote --tags --refs $REPO | tail -n1 | cut -d/ -f3) to get the latest tag as a variable.
  3. Finally we specify that as the 'branch' to clone

Tried this with a few repo's. No caveats come to mind (repo's without tags will fail of course), but do let me know if this can be improved.

Add anything you like, such as -c advice.detachedHead=false to not get such a long warning about the detached state.

Thermostat
  • 522
  • 4
  • 6
  • While this might be out of your control, repos that go through a versioning change will have issues. One example is `helix-editor/helix`. The latest is 22.08, and it's in the middle of the list. – sweenish Dec 21 '22 at 16:03
  • And the only workaround I have found is to just clone the repo first, then I'm able to checkout the latest tag with `--sort=taggerdate` added. It appears that all of the 'by date' sorting keys require object data, which means you can't just clone the latest tag. For my purposes, this is still close enough. – sweenish Dec 21 '22 at 16:23
0
# Clone repo
$ git clone <url>

# Go into repo folder
$ cd <reponame>

# Get new tags from the remote
$ git fetch --tags

# Get the latest tag name, assign it to a variable
$ latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)

# Checkout the latest tag
$ git checkout $latestTag

Found this solution here

Ram Vennam
  • 3,536
  • 1
  • 12
  • 19