6

我无法弄清楚如何删除远程分支。

我试图模仿以下 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

提前感谢您的想法和解决方案。

4

4 回答 4

15

这应该可以帮助您:

//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 测试

于 2012-09-12T09:35:32.810 回答
3

按照常规的 git 语法,你不应该RefSpec()是::branchToDelete

于 2012-08-10T05:16:42.923 回答
2

我从来没有这样做过,但是您是否只是DeleteBranchCommand通过指定来尝试 a origin/branchToDelete

编辑:我的意思是 Git/JGit 通过结构引用远程分支<remote name>/<branch name>(使用 ListBranchCommand 将帮助您确保拼写正确)。

要知道分支名称的确切拼写,您可以使用 a ListBranchCommand(不要忘记调用setListMode(REMOTE))。

注意:Git 允许比 JGit 更奇怪的行为,所以除非它写在某个地方,否则不要指望它们。

编辑:我的意思是 refspec 应该具有以下语法:(<remote branch>:<local branch>或者可能相反),但如果你错过了一端,不要指望它在 JGit 中有效,即使它在 Git 中有效。

于 2012-08-10T07:16:09.040 回答
0

我可以让它工作:

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

希望能帮助到你。

于 2012-10-09T12:15:53.803 回答