Max Shclover
Max Shclover

Reputation: 35

How to create a local branch based on another local branch in JGit

Let's say I have several branches in my local repository and I want to create another one based on one of them. In git I simply can do:

git branch new_branch old_branch 

In JGit I can try to make the old_branch current and then create new_branch, but is there an easier way? Am I missing something.

Upvotes: 0

Views: 544

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

In JGit, the start point for a new branch can be specified with setStartPoint.

For example:

git.branchCreate().
    setName("new-branch").
    setStartPoint("refs/heads/old-branch").
    call();

will create a branch named new-branch that points to the same commit as old-branch currently does.

If setStartPoint is omitted or set to null the new branch will point to HEAD.

I recommend using fully qualified ref-name, otherwise the outcome is undefined if the ref-name is ambiguous.

Upvotes: 2

Related Questions