JGit create new local branch and push to remote (branch does not exist on remote)

ptha picture ptha · Oct 16, 2015 · Viewed 9.5k times · Source

I'm working on an ANT task that calls some java that uses JGit to create a new branch on a git repository and push it to remote. I'm using JGit 2.1.0.

Here's the code:

CreateBranchCommand bcc;
CheckoutCommand checkout;
Git git;

try {
    Repository repo = new FileRepositoryBuilder().readEnvironment().findGitDir(src).build();
    git = new Git(repo);

    bcc = git.branchCreate();
    checkout = git.checkout();
} catch (IOException e) {
    throw new BuildException("Could not access repository " + src, e);
}

try {
        bcc.setName(branch)
            .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
            .setStartPoint("origin/" + branch)
            .setForce(force)
            .call();

        checkout.setName(branch);
        checkout.call();
}
} catch (Exception e) {
    throw new BuildException("Could not checkout repository " + src, e);
}

The src variable is set to the path of the git repo (which has already been cloned). The branch variable is set to: release_2_0_2 The force variable is set to: true or false (with both I have this issue).

There is a separate ANT task afterward to do the push.

When running the above code an exception is caught in the 2nd catch:

org.eclipse.jgit.api.errors.RefNotFoundException: Ref origin/release_2_0_2 can not be resolved

The issue seems to be with setStartPoint("origin/" + branch) If I hard code this to "origin/master" it works. The new branch is created. I'm simply trying to create a new branch locally and then push it to the remote. I was using https://stackoverflow.com/a/12928374/1860867 as an example.

Maybe I'm misunderstanding how the CreateBranchCommand should be used, all the examples I've seen are setting the startpoint to "origin/" + branch. Any suggestions/clarification would be helpful.

Answer

Rüdiger Herrmann picture Rüdiger Herrmann · Oct 16, 2015

Use setStartPoint() to specify what the new branch should be based upon. The commit to which setStartPoint points will be the initial commit for the new branch.

If the start point isn't explicitly specified, it defaults to HEAD. Please see the Git documentation for more details: https://git-scm.com/docs/git-branch

Hence the start point must exist - which the remote branch you are referring to clearly does not.

setUpstreamMode() isn't useful either if you are about to create a new branch. It is meant to configure tracking when you create a local branch for an existing remote branch. But since you don't have a remote branch yet there is no need to call setUpstreamMode.

Once you have created a local branch, use the PushCommand to publish it to the remote.

git.push()
    .setRemote("origin")
    .setRefSpecs(new RefSpec("release_2_0_2:release_2_0_2"))
    .call();

The above lines push the release_2_0_2 branch to the already known remote origin (the one you have cloned from).

The post How do I do git push with JGit? has more details on how to push with JGit.