JGit:签出远程分支
我正在使用JGit签出一个远程跟踪分支JGit:签出远程分支,git,jgit,Git,Jgit,我正在使用JGit签出一个远程跟踪分支 Git-binrepository=cloneCmd.call() CheckoutCommand checkoutCmd=binrepository.checkout(); checkoutCmd.setName(“origin/”+branchName); checkoutCmd.setUpstreamMode(CreateBranchCommand.setUpstreamMode.TRACK); checkoutCmd.setStartPoint(“
Git-binrepository=cloneCmd.call()
CheckoutCommand checkoutCmd=binrepository.checkout();
checkoutCmd.setName(“origin/”+branchName);
checkoutCmd.setUpstreamMode(CreateBranchCommand.setUpstreamMode.TRACK);
checkoutCmd.setStartPoint(“原点/”+branchName);
Ref=checkoutCmd.call();
文件已签出,但标头未指向分支。
以下是git状态的
$git状态
#目前不在任何分支上。
没有要提交的内容(工作目录清理)
同样的操作也可以在git命令行中执行,操作简单,而且很有效
git签出-t origin/mybranch
如何执行此操作?如的代码所示,您需要将布尔值createBranch
设置为true
,以便创建本地分支
您可以在中看到一个示例
必须使用setCreateBranch
创建分支:
Ref ref = git.checkout().
setCreateBranch(true).
setName("branchName").
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
setStartPoint("origin/" + branchName).
call();
您的第一个命令相当于git checkout origin/mybranch
(编辑:我向JGit提交了一个补丁,以改进CheckoutCommand:)无论出于何种原因,robinst发布的代码对我不起作用。特别是,创建的本地分支没有跟踪远程分支。这就是我使用的对我有用的东西(使用jgit 2.0.0.201206130900-r):
你也可以这样
git.checkout().setName(remoteBranch).setForce(true).call();
logger.info("Checkout to remote branch:" + remoteBranch);
git.branchCreate()
.setName(branchName)
.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint(remoteBranch)
.setForce(true)
.call();
logger.info("create new locale branch:" + branchName + "set_upstream with:" + remoteBranch);
git.checkout().setName(branchName).setForce(true).call();
logger.info("Checkout to locale branch:" + branchName);
我试过了。它起作用了。这是一个简单的解决办法。我需要进行更改,提交并推送到远程。我将测试并更新线程。比我的答案更完整+1@AdamSiemion你这是什么意思setStartPoint
应该与标记名一起使用,否则这是JGit中的一个错误(请报告)。@robinst要使它与git标记名一起使用,必须删除origin/
,否则会发生这种情况。@AdamSiemion:是的,但这与命令行上的相同,标记的名称空间与分支不同。链接到测试用例的好主意,+1:)这应该是可以接受的答案。其他两个解决方案不起作用。
git.pull().setCredentialsProvider(user).call();
git.branchCreate().setForce(true).setName(branch).setStartPoint("origin/" + branch).call();
git.checkout().setName(branch).call();
git.checkout().setName(remoteBranch).setForce(true).call();
logger.info("Checkout to remote branch:" + remoteBranch);
git.branchCreate()
.setName(branchName)
.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint(remoteBranch)
.setForce(true)
.call();
logger.info("create new locale branch:" + branchName + "set_upstream with:" + remoteBranch);
git.checkout().setName(branchName).setForce(true).call();
logger.info("Checkout to locale branch:" + branchName);