0
FetchResult fr = git.fetch().setCredentialsProvider(credentials).setCheckFetchedObjects(true).Call();
git.checkout().setCreateBranch(true).setName("origin/" + branchName).setStartPoint("origin/" + branchName + "path/to/folder").call()

This is the code I'm using to check out a single folder from a remote repository. Equivalent git commands are:

git fetch origin
git checkout origin/branch -- path/to/folder

But, the Java code doesn't work for me, I was only able to initialise the local repository and configure remote repository. The checkout didn't work and I couldn't find out what mistake I'm making.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
sabari
  • 41
  • 7
  • Possible duplicate of [Is partial checkout supported by JGit 3.7?](http://stackoverflow.com/questions/29505919/is-partial-checkout-supported-by-jgit-3-7) – Rüdiger Herrmann Apr 18 '17 at 06:27

2 Answers2

1

In order to check out a particular folder with JGit (a sparse checkout), you need to tell the CheckoutCommand which folder to check out.

For example:

git.checkout().setName("branch-to-check-out").addPath("path/to/folder").call();

addPath() can be called multiple times to check out each of the given paths. The path is interpreted relative to the work directory.

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
0

Try making changes in the checkout part like this

"origin/"

and give it a try.

Otherwise you want to do is a sparse checkout

How do I implement sparse checkout in JGit?

Community
  • 1
  • 1
zachi
  • 374
  • 2
  • 3
  • 12
  • I tried it, no use. And as well , I only want to checkout a particular folder from a unique branch, and checking out straight away from origin/ would give me the entire merged code, wouldn't it? And I don't want that. – sabari Apr 17 '17 at 05:48
  • I think what you want to do is a `sparse checkout`. Pls check this link https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History – zachi Apr 17 '17 at 06:07