我的Git存储库在根目录下有两个子目录:

/finisht
/static

当这是在SVN中时,/finisht在一个地方签出,而/static在其他地方签出了,如下所示:

svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static

有没有办法用Git做到这一点?


当前回答

@Chronial的anwser不再适用于最近的版本,但它是一个有用的Anwsr,因为它提出了一个脚本。

考虑到我收集的信息以及我只想签出分支的子目录这一事实,我创建了以下shell函数。它只获取分支中提供的目录的最新版本的浅拷贝。

function git_sparse_clone_branch() (
  rurl="$1" localdir="$2" branch="$3" && shift 3

  git clone "$rurl" --branch "$branch" --no-checkout "$localdir" --depth 1  # limit history
  cd "$localdir"

  # git sparse-checkout init --cone  # fetch only root file

  # Loops over remaining args
  for i; do
    git sparse-checkout set "$i"
  done

  git checkout "$branch"
)

因此,示例使用:

git_sparse_clone_branch git@github.com:user/repo.git localpath branch-to-clone path1_to_fetch path2_to_fetch

在我的案例中,克隆“仅”为23MB,而完整克隆为385MB。

使用git版本2.36.1进行测试。

其他回答

编辑:从Git 2.19开始,这终于是可能的,从这个答案中可以看出。

考虑对这个答案投赞成票。

注意:在Git2.19中,只实现了客户端支持,服务器端支持仍然缺失,因此它只在克隆本地存储库时有效。还要注意,大型Git宿主(例如GitHub)实际上并不使用Git服务器,而是使用自己的实现,因此即使Git服务器中显示了支持,也不会自动表示它在Git宿主上运行。(OTOH,因为他们不使用Git服务器,所以在Git服务器出现之前,他们可以在自己的实现中更快地实现它。)


不,这在Git中是不可能的。

在Git中实现这样的东西将是一项巨大的努力,这意味着客户端存储库的完整性将无法再得到保证。如果您感兴趣,请在gitmailinglist上搜索有关“稀疏克隆”和“稀疏获取”的讨论。

一般来说,Git社区的共识是,如果您有几个目录总是独立检出,那么这是两个不同的项目,应该存在于两个不同存储库中。您可以使用Git子模块将它们粘在一起。

对于macOS用户

对于zsh用户(特别是macOS用户)使用ssh克隆Repos,我只需要根据@Ciro Santilli的回答创建一个zsh命令:

要求:git的版本很重要。由于--sparse选项,它在2.25.1上不起作用。尝试将git升级到最新版本。(例如测试2.36.1)

示例用法:

git clone git@github.com:google-research/google-research.git etcmodel

代码:

function gitclone {
    readonly repo_root=${1?Usage: gitclone repo.git sub_dir}
    readonly repo_sub=${2?Usage: gitclone repo.git sub_dir}
    echo "-- Cloning $repo_root/$repo_sub"
    git clone \
      --depth 1 \
      --filter=tree:0 \
      --sparse \
      $repo_root \
    ;
    repo_folder=${repo_root#*/}
    repo_folder=${repo_folder%.*}
    cd $repo_folder
    git sparse-checkout set $repo_sub
    cd -
}


gitclone "$@"

我为执行“稀疏签出”编写了.gitconfig[别名]。检查一下(没有双关语):

在Windows上运行cmd.exe

git config --global alias.sparse-checkout "!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p \"$L/.git/info\" && cd \"$L\" && git init --template= && git remote add origin \"$1\" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo \"$2\" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f"

否则:

git config --global alias.sparse-checkout '!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p "$L/.git/info" && cd "$L" && git init --template= && git remote add origin "$1" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo "$2" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f'

用法:

# Makes a directory ForStackExchange with Plug checked out
git sparse-checkout https://github.com/YenForYang/ForStackExchange Plug

# To do more than 1 directory, you have to specify the local directory:
git sparse-checkout https://github.com/YenForYang/ForStackExchange ForStackExchange Plug Folder

为了方便和存储,git-config命令被“缩小”了,但这里扩展了别名:

# Note the --template= is for disabling templates.
# Feel free to remove it if you don't have issues with them (like I did)
# `mkdir` makes the .git/info directory ahead of time, as I've found it missing sometimes for some reason
f(){
    [ "$#" -eq 2 ] && L="${1##*/}" L=${L%.git} || L=$2;
    mkdir -p "$L/.git/info"
        && cd "$L"
        && git init --template=
        && git remote add origin "$1"
        && git config core.sparseCheckout 1;
    [ "$#" -eq 2 ]
        && echo "$2" >> .git/info/sparse-checkout
        || {
            shift 2;
            for i; do
                echo $i >> .git/info/sparse-checkout;
            done
        };
    git pull --depth 1 origin master;
};
f

为了澄清这里的一些好答案,许多答案中概述的步骤假设您在某个地方已经有了远程存储库。

给定:现有的git存储库,例如。git@github.com:some user/fullrepo.git,其中包含一个或多个您希望独立于repo其余部分拉动的目录,例如名为app1和app2的目录

假设您有一个如上所述的git存储库。。。

然后:您可以运行以下步骤,从较大的存储库中仅提取特定目录:

mkdir app1
cd app1
git init
git remote add origin git@github.com:some-user/full-repo.git
git config core.sparsecheckout true
echo "app1/" >> .git/info/sparse-checkout
git pull origin master

我错误地认为必须在原始存储库上设置稀疏签出选项,但事实并非如此:在从远程提取之前,您需要在本地定义所需的目录。远程回购不知道或不关心您只想跟踪回购的一部分。

希望这一澄清对其他人有所帮助。

如果您从未计划与从中克隆的存储库交互,则可以执行完整的git克隆并使用

git filter-branch --subdirectory-filter <subdirectory>

这样,至少历史会被保存下来。