使用JGit从Git存储库中查看特定的修订版本

MrD*_*MrD 11 java jgit

我正在尝试使用jGit克隆存储库并签出特定的提交.

假设提交哈希是:1e9ae842ca94f326215358917c620ac407323c81.

我的第一步是:

// Cloning the repository
    Git.cloneRepository()
        .setURI(remotePath)
        .setDirectory(localPath)
        .call();
Run Code Online (Sandbox Code Playgroud)

然后我发现了另一个提出这种方法的问题:

git.checkout().
                setCreateBranch(true).
                setName("branchName").
                setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
                setStartPoint("origin/" + branchName).
                call();
Run Code Online (Sandbox Code Playgroud)

但我不确定如何将两者联系在一起?

有什么想法吗?

Rüd*_*ann 16

您必须首先克隆存储库,因此您的第一步是正确的:

Git.cloneRepository().setURI(remotePath).setDirectory(localPath).call();
Run Code Online (Sandbox Code Playgroud)

要通过其ID识别提交,您可以这样调用checkout:

git.checkout().setName("<id-to-commit>").call();
Run Code Online (Sandbox Code Playgroud)

但请注意,这将导致分离的HEAD.为避免这种情况,您可以checkout先创建一个指向提交的新分支,然后签出此分支.

git.checkout().setCreateBranch(true).setName("new-branch").setStartPoint("<id-to-commit>").call();
Run Code Online (Sandbox Code Playgroud)

API不是很直观,但它可以做到它应该做的事情.