w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Check out specific revision from Git repository with JGit

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.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.