简体   繁体   中英

How to clone a single file from git repository using JGIT

I have used below code to clone my repository and it works fine.

  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....");

     }

But I just want to download a single java file from the git repository instead of cloning the entire repo. But not sure how can I do that.

The code below works fine for me :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM