Check out specific revision from Git repository with JGit

后端 未结 1 760
走了就别回头了
走了就别回头了 2020-12-10 04:02

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

Assuming the commit hash is: 1e9ae842ca94f326215358917c620ac407323c81.

My fir

相关标签:
1条回答
  • 2020-12-10 04:32

    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.

    0 讨论(0)
提交回复
热议问题