JGit – Checkout to specific remote commit ID hash

Issue

I’m trying to access specific commitId files using the JGit library.

Using Git command this would look like : git checkout [COMMIT_ID], then my folder would checkout to the specific commit and get any file from it.

Now using JGit, I’m calling the Git.cloneRepository() function to get my repository (can’t clone from a specific commitId here I think sadly). Then I’m trying to checkout using this : gitRepo.checkout().setName(gitCommitId).call()

But this is getting me the folliwing error : Remote origin did not advertise Ref for branch COMMIT_ID. This Ref may not exist in the remote or may be hidden by permission settings.

Which is odd because the CLI git command does work.

Maybe it is not something feasible through this lib but I didn’t find anything else on the web yet.

Solution

setName(String name) is more for branch name, not for commit ID.

setStartPoint(RevCommit startCommit) does use a commit ID.

As seen here, git.checkout().setAllPaths(true).setStartPoint(gitCommitId).call(); would work better after your clone.

Answered By – VonC

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published