如何在 Git 存储库中克隆最新标签

Pre*_*ngh 6 git

git ls-remote --tags git://github.com/git/git.git
Run Code Online (Sandbox Code Playgroud)

列出最新的标签,无需克隆。我需要一种能够直接从最新标签克隆的方法

Tom*_*ale 8

调用这个~/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)

  • @Cris70 不是这样,`git`在内部查找名为`git-*`的脚本,使用`git clone-latest-tXX`也会给你`最相似的命令是clone-latest-tag` (2认同)

Ram*_*nam 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
Run Code Online (Sandbox Code Playgroud)

在这里找到了这个解决方案