Epicurus Epicurus - 1 year ago 97
Git Question

Reset to specified commit using JGit

I'm using JGit to programmatically pull remote changes to a local repository. If the pull fails and throws an exception, I want to reset the local repository to the latest commit before the pull attempt.

However, the ResetCommand does not take a commit identifier as an argument, even though the Git reset command accepts a "commit" parameter. Is this a known limitation of the ResetCommand, or am I missing something?

Is there an alternative JGit command or sequence of commands that could be used to the same effect?

Answer Source

The setRef() method of the ResetCommand allows specifying the id of a commit. For example:

resetCommand.setRef( "< SHA-1 >" );

A pull can fail for several reasons. I assume that with 'pull fails and throws an exception' you refer to a pull that cannot be merged with the current local branch.

If your intention is to prevent a failing merge affecting the current repository state, you may want to first fetch the upstream changes and then follow the suggestion I made here: How to get conflicting lines with JGit

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download