简体   繁体   中英

Checking out a Git repository by branch or commit ID using JGit

I am currently cloning repositories with JGit using the following:

Git.cloneRepository()
    .setURI(uri)
    .setDirectory(createTempDir())
    .setBranchesToClone(singleton("refs/heads/" + branch))
    .setBranch("refs/heads/" + branch)
    .call();

However I would like to allow branch to also be a commit ID. How could I implement this?

While cloning a Git repository, you can only specify branches (or more generally references) to be cloned. This is not specific to JGit but also applies to CLI Git.

A remote repository can be queried for the available references along with their commit IDs, but an arbitrary commit ID cannot be cloned.

With JGit, the LsRemoteCommand can be used to obtain all available references without actually downloading the respective history. Or you can specify a pre-known reference with the CloneCommand in order to clone the given reference along with its history.

If performance and bandwidth aren't an issue, you could, of course, clone a repository with all its references ( cloneCommand.setCloneAllBranches( true ) ) and once it is available locally, check out the desired commit.

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