git ls-remote --tags git://github.com/git/git.git
Run Code Online (Sandbox Code Playgroud)
列出最新的标签,无需克隆。我需要一种能够直接从最新标签克隆的方法
调用这个~/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 "$@"
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
% 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.
...
Run Code Online (Sandbox Code Playgroud)
# 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
Run Code Online (Sandbox Code Playgroud)
在这里找到了这个解决方案