JGit validation of previous commit

How do I tell JGit how its parent is? For example, if I have a situation similar to the one below in the master branch:

c815b27 newestCommit (HEAD -> master, origin/master, master)
e46dcaf previousCommit
b2d6867 previousPreviousCommit

      

I would like to call a command from JGit that would look something like this:

git.checkout().setName("c815b27~").call();

      

and will result in a state where HEAD is moved to commit e46dcaf

:

c815b27 newestCommit (origin/master, master)
e46dcaf previousCommit (HEAD)
b2d6867 previousPreviousCommit

      

However, when I call the above statement, nothing happens. I also came across the following statement, which also doesn't move the HEAD:

git.checkout().setStartPoint("c815b27~").call();

      

Any ideas on how to achieve a previous commit based on tilde (~) or caret (^) characters and is this possible using the JGit API?

+3


source to share


1 answer


First, you need to resolve an expression that points to a previous commit. Then you can check the resulting commit ID.

For example:



ObjectId previousCommitId = git.getRepository().resolve( "HEAD^" );
git.checkout().setName( previousCommitId ).call();

      

Note that checking the commit disables HEAD .

+3


source







All Articles