我想以編程方式更新HEAD而不對非裸倉庫執行checkout或rebase.
我希望工作樹和索引在操作后保持不變.
編輯
我需要更新HEAD的符號目標,而不是HEAD當前目標的提交ID.這更像是一個結賬,而不是其他任何東西,除了我不能使用org.eclipse.jgit.api.CheckoutCommand因為它需要我更新路徑,但我不想觸摸工作樹. org.eclipse.jgit.api.CreateBranchCommand也不合適,因為它需要一個特定的起點,因為我正在創建一個孤兒分支,所以它不存在.
解決方法:
例:
Result updateHead(
Repository repo, String newHead, boolean force, boolean detach
) throws IOException {
RefUpdate refUpdate = repo.getRefDatabase().newUpdate(Constants.HEAD, detach);
refUpdate.setForceUpdate(force);
return refUpdate.link(newHead);
}
答案隱藏在jgit源代碼中的大約5個位置.
jgit v2.0.0.201206130900-r中的三個api命令為您更新HEAD:clone,checkout和rebase.如果適用,請使用其中之一.
這些都不適用:checkout和rebase會改變工作樹和索引.
希望發布這個問題和答案將節省其他人我花在它上面的時間.
標簽:jgit,java,git
來源: https://codeday.me/bug/20190826/1727080.html