让现有的Git分支跟踪JGit的远程分支? [英] Make an existing Git branch track a remote branch with JGit?

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

问题描述

我使用JGit创建一个新的git存储库,并且文件夹中已经存在的所有东西,我保存为新分支。



我正在使用

Git.init()。setDirectory(workingDir).call();

(默认主分支是在上述语句之后创建的,因此我将其重命名为backupBranch) 。因为主人以后我会从远程主服务器克隆。



问题是,当我推送backupbranch时,它被推送,但无法建立远程跟踪。



来自终端:如果我使用 git分支-vv 结果为

 master 5f1994c [origin / master] commitmsg 
backupbranch 7a4d671在MyTests文件夹中已经备份文件..

根据链接让一个现有的Git分支跟踪一个远程分支?我可以使用
git branch -u upstream / foo 从终端指定推后的任何时间backupbranch。



如何使用JGit实现它。



以下是我使用的代码



创建备份分支


$ b

  git.branchRename() .setNewName(backUpName).CALL(); 
git.add()。addFilepattern(。)。call();
RevCommit commit = git.commit()。setMessage(在MyTests文件夹中已经备份文件..)。

然后第一次推送。

  Iterable< PushResult> pushResult = git.push()。call(); 

请建议我可以指定现有分支的远程追踪方式。

解决方案

您可以使用 CreateBranchCommand 本文所述

  CreateBranchCommand create = git.branchCreate(); 
create.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
create.setName(develop);
create.setStartPoint(origin / develop);
create.setForce(true)
create.call();

请注意 setForce(true)调用。由于您的本地分支已经存在,您将需要覆盖它。之后它会指向远程跟踪分支( origin / develop ) - 它在之前完成了它。

另外你可以直接修改配置文件。例如:
$ b

  StoredConfig config = git.getRepository()。getConfig(); 
String branchName =develop
String remoteName =origin;
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,branchName,ConfigConstants.CONFIG_KEY_REMOTE,remoteName);
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION,branchName,ConfigConstants.CONFIG_KEY_MERGE,Constants.R_HEADS + branchName);
config.save();

这将导致如下配置部分:

  [branchdevelop] 
remote = origin
merge = refs / heads / develop
pre>

I am using JGit for creating a new git repository, and everything already present in the folder, I save as new branch.

I am using

Git.init().setDirectory(workingDir).call();

(by-default master branch is created after above statement, so i rename it to "backupBranch"). because master I clone from a remote master later.

Problem is when I push the backupbranch, its pushed but no remote tracking I am able to establish.

from terminal: if I use git branch -vv result is

master       5f1994c [origin/master] commitmsg
backupbranch 7a4d671 taking backup of file already in MyTests Folder..

According to link Make an existing Git branch track a remote branch? I can use git branch -u upstream/foo from terminal to specify any time after pushing backupbranch.

How to achieve it using JGit.

Following is code I am using

to create the backupbranch

git.branchRename().setNewName(backUpName).call();
git.add().addFilepattern(".").call();
RevCommit commit = git.commit().setMessage("taking backup of file already in MyTests Folder..").call();

then push it first time.

Iterable<PushResult> pushResult = git.push().call();

Please suggest the way I can specify remote tracking for existing branch.

解决方案

You can use the CreateBranchCommand as decribed in this post

CreateBranchCommand create = git.branchCreate();
create.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
create.setName("develop");
create.setStartPoint("origin/develop");
create.setForce(true)
create.call();

Note the setForce(true) call. Because your local branch already exists you will need to override it. It will point to the remote tracking branch (origin/develop) afterwards - which it did before anyway.

Alternatively you can directly modify the config file. For example:

StoredConfig config = git.getRepository().getConfig();
String branchName = "develop"
String remoteName = "origin";
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,  ConfigConstants.CONFIG_KEY_REMOTE, remoteName);
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName, ConfigConstants.CONFIG_KEY_MERGE, Constants.R_HEADS + branchName);
config.save();

This will result in a configuration section like this:

[branch "develop"]
remote = origin
merge = refs/heads/develop

这篇关于让现有的Git分支跟踪JGit的远程分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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