Check out specific revision from Git repository with JGit

∥☆過路亽.° 提交于 2019-11-27 05:55:50

问题


I am trying to use jGit to clone a repository and checkout a particular commit.

Assuming the commit hash is: 1e9ae842ca94f326215358917c620ac407323c81.

My first step is:

// 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?

Any thoughts?


回答1:


You will have to clone the repository first, thus your first step was right:

Git.cloneRepository().setURI(remotePath).setDirectory(localPath).call();

To just checkout a commit by its id you can call checkout like this:

git.checkout().setName("<id-to-commit>").call();

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();

The API isn't very intuitive, but it does what it should.



来源:https://stackoverflow.com/questions/24892748/check-out-specific-revision-from-git-repository-with-jgit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!