[英]git new local branch creation then push remote branch(new remote) and then set upstream using JGIT
I am going to dynamically create local branches(like Hotfix_Test1, Hotfix_Test2 each at different time) and push it to remote. 我将动态创建本地分支(分别在不同时间创建Hotfix_Test1,Hotfix_Test2)并将其推送到远程。
These branches should contain the source available in a branch called Release which is another local 这些分支应包含名为Release的分支中可用的源,该分支是另一个本地
Release is already pushed to remote from local using git commands 版本已经使用git命令从本地推送到远程
git push --set-upstream origin Release git push --set-upstream原始版本
I create a git object and trying the following code to create hotfix branches dynamically 我创建了一个git对象,并尝试以下代码来动态创建修补程序分支
public static void main(String[] args) { CreateBranchCommand createBranchCommand = null; CheckoutCommand checkoutCommand = null; Git git = null; String releaseVersion = "Test"; PushCommand pushCommand = null; StoredConfig config = null; try { /* Consider Git object is created */ git = createGitObject(); /* Checkout Release branch */ checkoutCommand = git.checkout(); checkoutCommand.setName("Release"); checkoutCommand.call(); /* Creating Hotfix Branch */ createBranchCommand = git.branchCreate(); createBranchCommand.setName("hotfix_" + releaseVersion).call(); /* Pushing Hotfix Branch to remote * note that the hotfix is not present in remote */ pushCommand = git.push(); pushCommand.setRemote("origin"); pushCommand.setRefSpecs(new RefSpec("hotfix_" + releaseVersion + ":hotfix_" + releaseVersion)); pushCommand.call(); /* Trying to set upstream for newly created hotfix branch */ createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM); createBranchCommand.setStartPoint("origin/" + "hotfix_" + releaseVersion); createBranchCommand.setForce(true); createBranchCommand.call(); checkoutCommand.setName("hotfix_" + releaseVersion); checkoutCommand.call(); /* Editing the configuration file in local */ config = git.getRepository().getConfig(); config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "hotfix_" + releaseVersion, "remote", "origin"); config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "hotfix_" + releaseVersion, "merge", "refs/heads/hotfix_" + releaseVersion); config.save(); } catch (Exception exception) { exception.printStackTrace(); } }
when the execution comes to this line 当执行到这一行时
'createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);' 'createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);'
it is throwing exception 它抛出异常
i dont know wats the mistake. 我不知道会出错。 Kindly help with the error. 请帮助解决错误。
You already called createdBranchCommand: 您已经调用了createdBranchCommand:
/* Creating Hotfix Branch */
createBranchCommand = git.branchCreate();
createBranchCommand.setName("hotfix_" + releaseVersion).call();
then did a push before trying to re-use that branchCreate() command again: 然后先进行一次推送,然后再尝试再次使用该branchCreate()命令:
/* Trying to set upstream for newly created hotfix branch */
createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
createBranchCommand.setStartPoint("origin/" + "hotfix_" + releaseVersion);
createBranchCommand.setForce(true);
createBranchCommand.call();
Try creating the branch and setting its upstream config in one go, then push to it after: 尝试创建分支并一次性设置其上游配置,然后在之后推送至该分支:
/* Creating Hotfix Branch */
createBranchCommand = git.branchCreate();
createBranchCommand.setName("hotfix_" + releaseVersion);
createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
createBranchCommand.setStartPoint("origin/" + "hotfix_" + releaseVersion);
createBranchCommand.setForce(true);
createBranchCommand.call();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.