I want to update HEAD programmatically without performing a checkout or rebase on a non-bare repo.
I want the working tree and index to be unchanged after the operation.
EDIT
I need to update the symbolic target of HEAD, not the commit ID of the current target of HEAD. This is more like a checkout than anything else, except I can’t use org.eclipse.jgit.api.CheckoutCommand because it requires that I update paths, but I don’t want to touch the working tree. org.eclipse.jgit.api.CreateBranchCommand is also inappropriate because it expects a particular starting point, which doesn’t exist because I’m creating an orphan branch.
This worked for me:
RefUpdate.link().Example:
The answer is buried in about 5 places in the jgit source code.
Three api commands in jgit v2.0.0.201206130900-r update HEAD for you: clone, checkout, and rebase. Use one of those if it applies.
None of those apply: checkout and rebase would change the working tree and index.
Hopefully posting this question and answer will save others the time I had to spend on it.