使用JGit从Git存储库中查看特定的修订版本 [英] Check out specific revision from Git repository with JGit
问题描述
我正在尝试使用jGit克隆存储库并签出特定提交。
I am trying to use jGit to clone a repository and checkout a particular commit.
假设提交哈希是:1e9ae842ca94f326215358917c620ac407323c81。
Assuming the commit hash is: 1e9ae842ca94f326215358917c620ac407323c81.
我的第一步是:
// Cloning the repository
Git.cloneRepository()
.setURI(remotePath)
.setDirectory(localPath)
.call();
然后我发现了另一个提出这种方法的问题:
I then found another question which suggested this approach:
git.checkout().
setCreateBranch(true).
setName("branchName").
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
setStartPoint("origin/" + branchName).
call();
但我不确定如何将两者联系在一起?
But I'm unsure how to link the two together?
有什么想法?
推荐答案
您必须首先克隆存储库,因此您的第一步是正确的:
You will have to clone the repository first, thus your first step was right:
Git.cloneRepository().setURI( remotePath ).setDirectory( localPath ).call();
要通过其ID识别提交,您可以致电 checkout
像这样:
To just checkout a commit by its id you can call checkout
like this:
git.checkout().setName( "<id-to-commit>" ).call();
但请注意,这将导致分离的HEAD 。为了避免这种情况,你可以告诉 checkout
首先创建一个指向提交的新分支,然后签出这个分支。
But note that this will result in a detached HEAD. To avoid this, you can tell checkout
to create a new branch first that points to the commit and then checkout this branch.
git.checkout().setCreateBranch( true ).setName( "new-branch" ).setStartPoint( "<id-to-commit>" ).call();
API不是很直观,但它可以做到应有的。
The API isn't very intuitive, but it does what it should.
这篇关于使用JGit从Git存储库中查看特定的修订版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!