如何使用 JGIT 从 git 存储库克隆单个文件

How to clone a single file from git repository using JGIT

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

  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 文件,而不是克隆整个存储库。但不确定我该怎么做。

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

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();