javabranchjgit

RefNotFoundException when creating a branch in JGIT


I have a problem where i cannot create local branch.

Error: org.eclipse.jgit.api.errors.RefNotFoundException: Ref origin/sideMerge cannot be resolved

I have checked the following topics and some others in Stackoverflow but it seems something fishy going on or something i don't understand yet.

Can someone point me to a direction what i don't understand regarding the Ref ? As far as i know the local refs starts from origin/"other_branch"

Code snippet:

Git git;
    CreateBranchCommand tmpCreateBranch;
    git = new Git(existingRepo);
    tmpCreateBranch = git.branchCreate();

    try {
        tmpCreateBranch.setName(tmpBranchName).setStartPoint("origin/" + tmpBranchName).setForce(true).call();
    } catch (Exception e) {
        System.out.println("Error in Branch creation");
        System.out.println(e);
    }

Solution

  • According to Git manual, you can create a branch using the following syntax. It creates a new branch head named <branchname> which points to the current HEAD, or <start-point> if given:

    git branch [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
    

    Therefore, you can create a local branch "topic" by following the similar syntax in JGit (see next code-block). In this case, you didn't configure the start-point explicitly. JGit will use HEAD as start-point. So everything works fine:

    git.branchCreate().setName("topic").call();
    

    However, if you create a local branch with start-point origin/topic, JGit will try to find this reference in Git References. In this context, origin is the name of the remote, and topic is the name of the branch. If not found, it raises an exception:

    git.branchCreate().setName("topic").setStartPoint("origin/topic").call();
    

    You also need to be aware that: