首页 > 解决方案 > 如何使用 JGIT 从 git 存储库中克隆单个文件

问题描述

我使用下面的代码来克隆我的存储库,它工作正常。

  public static void getCodeBase() throws Exception 
     {
        String url="https://urltogitrepository";
        String destination = ".//clone3//";   
        Git.cloneRepository().setURI(url)
              .setDirectory(Paths.get(destination).toFile()).call();
        System.out.println("Cloned successfully....");

     }

但我只想从 git 存储库下载一个 java 文件,而不是克隆整个 repo。但不知道我该怎么做。

标签: javajgit

解决方案


下面的代码对我来说很好:

Git repo = Git.cloneRepository()
          .setURI(url)
          .setDirectory(destination))
          .setBranchesToClone(Arrays.asList("refs/heads/master"))
          .setCloneAllBranches(false)
          .setCloneSubmodules(true)
          .setNoCheckout(true)
          .call();

         repo.checkout().setStartPoint("origin/master").addPath("file1.txt").call();

推荐阅读