git 创建新的本地分支然后推送远程分支(新远程)然后使用 JGIT 设置上游
git new local branch creation then push remote branch(new remote) and then set upstream using JGIT
我要动态创建本地分支(比如
Hotfix_Test1、Hotfix_Test2 各在不同时间)并将其推送到远程。
这些分支应包含名为 Release 的分支中可用的源,该分支是另一个本地
版本已经使用 git 命令从本地推送到远程
- git checkout -b 发布git push
git push --set-upstream origin Release
我创建了一个 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();
}
}
当执行到这一行时
'createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);'
它抛出异常
- java.lang.IllegalStateException:命令 org.eclipse.jgit.api.CreateBranchCommand 在错误的状态下调用
我不知道错在哪里。请帮忙解决错误。
您已经调用了 createdBranchCommand:
/* Creating Hotfix Branch */
createBranchCommand = git.branchCreate();
createBranchCommand.setName("hotfix_" + releaseVersion).call();
然后在再次尝试重新使用该 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();
尝试一次创建分支并设置其上游配置,然后推送到它:
/* 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();
我要动态创建本地分支(比如 Hotfix_Test1、Hotfix_Test2 各在不同时间)并将其推送到远程。
这些分支应包含名为 Release 的分支中可用的源,该分支是另一个本地
版本已经使用 git 命令从本地推送到远程
- git checkout -b 发布git push
git push --set-upstream origin Release
我创建了一个 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(); } }
当执行到这一行时
'createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);'
它抛出异常
- java.lang.IllegalStateException:命令 org.eclipse.jgit.api.CreateBranchCommand 在错误的状态下调用
我不知道错在哪里。请帮忙解决错误。
您已经调用了 createdBranchCommand:
/* Creating Hotfix Branch */
createBranchCommand = git.branchCreate();
createBranchCommand.setName("hotfix_" + releaseVersion).call();
然后在再次尝试重新使用该 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();
尝试一次创建分支并设置其上游配置,然后推送到它:
/* 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();