如何克隆,获取或稀疏检出git存储库中的单个目录或目录列表? [英] How do I clone, fetch or sparse checkout a single directory or a list of directories from git repository?

查看:183
本文介绍了如何克隆,获取或稀疏检出git存储库中的单个目录或目录列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从git存储库中克隆,获取或稀疏检出单个文件或目录或文件或目录列表,从而避免下载整个历史记录或至少保持历史记录下载量最少?

How do I clone, fetch or sparse checkout a single file or directory or a list of files or directories from a git repository avoiding downloading the entire history or at least keeping history download at minimum?

为了人们在这里着陆的利益,以下是对其他类似问题的引用:

For the benefit of people landing here, these are references to other similar questions:

  • How do I clone a subdirectory only of a Git repository?
  • How to sparsely checkout only one single file from a git repository?

这些类似的问题大约在10年前问过,此后git不断发展,最终导致大量不同的答案,取决于所考虑的git版本,有些答案更好或更糟.麻烦的是,上述问题中没有一个答案能满足所有这些问题的所有要求,这意味着您必须阅读所有答案,然后自己编写自己的答案,最终才能满足所有要求.

These similar questions were asked some 10 years ago and git evolved ever since, which ended up causing a flood of different answers, some better, some worse, depending on the version of git being considered. The trouble is that not a single answer from these aforementioned questions attend all requirements from these questions combined, which means that you have to read all answers and compile in your head your own answer which eventually attend all requirements.

此问题在前面提到的问题的基础上进行了扩展,比其他问题加起来提出了更加灵活和严格的要求.

This question here expands on previous questions mentioned, imposing more flexible and stringent requirements than other questions combined.

推荐答案

下面的bash函数可以解决问题.

This bash function below does the trick.

function git_sparse_checkout {
    # git repository, e.g.: http://github.com/frgomes/bash-scripts
    local url=$1
    # directory where the repository will be downloaded, e.g.: ./build/sources
    local dir=$2
    # repository name, in general taken from the url, e.g.: bash-scripts
    local prj=$3
    # tag, e.g.: master
    local tag=$4
    [[ ( -z "$url" ) || ( -z "$dir" ) || ( -z "$prj" ) || ( -z "$tag" ) ]] && \
        echo "ERROR: git_sparse_checkout: invalid arguments" && \
        return 1
    shift; shift; shift; shift

    # Note: any remaining arguments after these above are considered as a
    # list of files or directories to be downloaded.

    mkdir -p ${dir}
    if [ ! -d ${dir}/${prj} ] ;then
        mkdir -p ${dir}/${prj}
        pushd ${dir}/${prj}
        git init
        git config core.sparseCheckout true
        local path="" # local scope
        for path in $* ;do
            echo "${path}" >> .git/info/sparse-checkout
        done
        git remote add origin ${url}
        git fetch --depth=1 origin ${tag}
        git checkout ${tag}
        popd
    fi
}

这是如何使用它的示例:

This is an example of how to use it:

function example_download_scripts {
  url=http://github.com/frgomes/bash-scripts
  dir=$(pwd)/build/sources
  prj=bash-scripts
  tag=master
  git_sparse_checkout $url $dir $prj $tag "user-install/*" sysadmin-install/install-emacs.sh
}

在上面的示例中,请注意目录必须后跟/*,并且必须在单引号或双引号之间.

In the example above, notice that a directory must be followed by /* and must be between single quotes or double quotes.

这篇关于如何克隆,获取或稀疏检出git存储库中的单个目录或目录列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆