簡體   English   中英

如何使用 JGit 刪除遠程分支

[英]How to remove remote branch with JGit

我無法弄清楚如何刪除遠程分支。

我試圖模仿以下 GIT 命令: git push origin :branchToDelete

以下代碼及其與空源的變體:

RefSpec refSpec = new RefSpec();
refSpec = refSpec.setSource("");
// remove branch from origin:
git.push().setRefSpecs(refSpec).add(branchToDelete).call();

拋出和異常,如:

org.eclipse.jgit.api.errors.JGitInternalException: Exception caught during execution of push command
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:175)
    at org.gitscripts.DeleteBranchOperation.execute(DeleteBranchOperation.java:27)
    at org.gitscripts.Main.main(Main.java:27)
Caused by: java.io.IOException: Source ref  doesnt resolve to any object.
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:285)
    at org.eclipse.jgit.transport.RemoteRefUpdate.<init>(RemoteRefUpdate.java:189)
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:612)
    at org.eclipse.jgit.transport.Transport.findRemoteRefUpdatesFor(Transport.java:1150)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:149)
    ... 2 more

在此先感謝您的想法和解決方案。

這應該可以幫助您:

//delete branch 'branchToDelete' locally
git.branchDelete().setBranchNames('refs/heads/branchToDelete').call();

//delete branch 'branchToDelete' on remote 'origin'
RefSpec refSpec = new RefSpec()
        .setSource(null)
        .setDestination("refs/heads/branchToDelete");
git.push().setRefSpecs(refSpec).setRemote('origin').call();

用 jgit 2.0.0.201206130900-r 測試

按照常規的 git 語法,您的RefSpec()不應該是: :branchToDelete嗎?

我從來沒那么做,但你只是嘗試DeleteBranchCommand通過指定origin/branchToDelete

編輯:我的意思是 Git/JGit 通過結構<remote name>/<branch name>引用遠程分支(並且使用 ListBranchCommand 將幫助你確保你得到正確的拼寫)。

要知道分支名稱的確切拼寫,您可以使用ListBranchCommand (不要忘記調用setListMode(REMOTE) )。

注意:Git 允許比 JGit 更奇怪的行為,所以除非它被寫在某個地方,否則不要指望它們。

編輯:我的意思是 refspec 應該具有以下語法: <remote branch>:<local branch> (或可能相反),但不要指望它在 JGit 中工作,如果你錯過了一端,即使它適用於 Git。

我可以讓它工作:

StoredConfig config = git.getRepository().getConfig();
config.unsetSection("remote", "origin");
try {
    config.save();
} catch (IOException e) {
    logger.error(e.getMessage());
}

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM