Search code examples
gitbranchgit-remotejgitgit-checkout

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.


Solution

  • 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();