Buddy
Buddy

Reputation: 152

how to create git branch in remote repository without checking out using jgit

Can we create a remote branch in git using jgit without checking out. For example I want to create a branch named foo from branch named bar in my remote repository without checking out branch bar locally.

Upvotes: 3

Views: 2636

Answers (3)

Enosh Bansode
Enosh Bansode

Reputation: 1778

I was able to create a remote branch using the rest api curl command

/rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches

Rest api and curl command

Upvotes: 0

SunghoMoon
SunghoMoon

Reputation: 1461

You cannot create remote branch without cloning.
But you can create remote branch without checking-out.

To be specific, you can clone just '.git' directory(not working directory) with "setNoCheckout(true)," and then create remote branch and push.

Let's see my example:

// clone repository. Like to 'git clone -n repository'
Git.cloneRepository().setURI(projectURL).setDirectory(repositoryFile)
  .setCredentialsProvider(credentials).setCloneAllBranches(true).setNoCheckout(true)
  .call();

// create branch locally
git.branchCreate().setName(targetBranch).setForce(true)
  .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).setStartPoint(startPoint)
  .call();

// push created branch to remote repository
// This matches to 'git push targetBranch:targetBranch'
RefSpec refSpec = new RefSpec().setSourceDestination(targetBranch, targetBranch);   
git.push().setRefSpecs(refSpec).setCredentialsProvider(credentials).call();

I use jgit version 3.3.0.201403021825-r. And this works fine for me.

Upvotes: 4

VonC
VonC

Reputation: 1323723

You need to have at least one branch 'bar' in your local repo in order to push it under a different name 'foo'.
But you don't have to checkout that local branch 'bar' first.

See this JGit push test example:

RefSpec spec = new RefSpec("refs/heads/bar:refs/heads/foo");
git1.push().setRemote("test").setRefSpecs(spec).call();

Upvotes: 2

Related Questions