git JGit:签出远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12927163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:39:23  来源:igfitidea点击:

JGit: Checkout a remote branch

gitjgit

提问by Nambi

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

我正在使用 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();

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

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

$ 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 命令行中执行,很容易并且可以工作,

git checkout -t origin/mybranch

How to do this JGit?

如何做这个 JGit?

回答by robinst

You have to use setCreateBranchto create a branch:

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

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.

你的第一个命令相当于git checkout origin/mybranch.

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

(编辑:我向 JGit 提交了一个补丁以改进 CheckoutCommand 的文档:https: //git.eclipse.org/r/8259

回答by Jason Wheeler

For whatever reason, the code that robinst posted did not work for me. In particular, the local branch that was created did not track the remote branch. This is what I used that worked for me (using jgit 2.0.0.201206130900-r):

无论出于何种原因,robinst 发布的代码对我不起作用。特别是,创建的本地分支没有跟踪远程分支。这是我使用的对我有用的东西(使用 jgit 2.0.0.201206130900-r):

git.pull().setCredentialsProvider(user).call();
git.branchCreate().setForce(true).setName(branch).setStartPoint("origin/" + branch).call();
git.checkout().setName(branch).call();

回答by VonC

As shown in the code of CheckoutCommand, you need to set the boolean createBranchto truein order to create a local branch.

作为的代码所示CheckoutCommand,您需要将布尔设置createBranchtrue以创造一个地方分支。

You can see an example in CheckoutCommandTest- testCreateBranchOnCheckout()

你可以看到一个例子CheckoutCommandTest-testCreateBranchOnCheckout()

@Test
public void testCreateBranchOnCheckout() throws Exception {
  git.checkout().setCreateBranch(true).setName("test2").call();
  assertNotNull(db.getRef("test2"));
}

回答by u8000980

you also can just like this

你也可以这样

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