JGit:签出一个远程分支 [英] JGit: Checkout a remote branch

查看:1510
本文介绍了JGit:签出一个远程分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JGit来检出一个远程追踪分支。

Git binrepository = cloneCmd .call()

CheckoutCommand checkoutCmd = binrepository.checkout();
checkoutCmd.setName(origin /+ branchName);
checkoutCmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK);
checkoutCmd.setStartPoint(origin /+ branchName);

Ref ref = checkoutCmd.call();

文件已签出,但HEAD未指向分支。
以下是 git status 输出,


$ b

  $ git status 
#目前没有任何分支。
没有提交(工作目录清理)

相同的操作可以在git命令中执行线,很容易,它的工作原理,

git checkout -t origin / mybranch

如何执行此JGit?

解决方案

您必须使用 setCreateBranch 来创建一个分支:

  Ref ref = git.checkout()。 
setCreateBranch(true)。
setName(branchName)。
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)。
setStartPoint(origin /+ branchName)。
call();

您的第一个命令相当于 git checkout origin / mybranch / r / 8259rel =noreferrer> https://git.eclipse.org/r/8259 )


I'm using JGit to checkout a remote tracking branch.

Git binrepository = cloneCmd.call()

CheckoutCommand checkoutCmd = binrepository.checkout();
checkoutCmd.setName( "origin/" + branchName);
checkoutCmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK );
checkoutCmd.setStartPoint( "origin/" + branchName );

Ref ref = checkoutCmd.call();

The files are checked out, but the HEAD is not pointing to the branch. Following is the git status output,

$ git status
# Not currently on any branch.
nothing to commit (working directory clean)

The same operation can be performed in git command line, easily and it works,

git checkout -t origin/mybranch

How to do this JGit?

解决方案

You have to use setCreateBranch to create a branch:

Ref ref = git.checkout().
        setCreateBranch(true).
        setName("branchName").
        setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
        setStartPoint("origin/" + branchName).
        call();

Your first command was the equivalent of git checkout origin/mybranch.

(Edit: I submitted a patch to JGit to improve the documentation of CheckoutCommand: https://git.eclipse.org/r/8259)

这篇关于JGit:签出一个远程分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆