JGit - 推送分支并添加上游(-u 选项)

JGit - Pushing a branch and add upstream (-u option)

JGit中,我搜索了一种推送分支并添加上游引用(跟踪)的方法。

是选项-u--set-upstream变成了push command

我在 class PushCommand 中没有看到允许这样做的方法。

拜托,你能告诉我怎么做吗?

PushCommand pushCommand = git.push()
                    .setRemote(remoteAlias)
                    .setRefSpecs(spec);

JGit PushCommand 不提供此功能(目前),但您可以像 --set-upstream 那样修改存储库配置。

如果您将远程别名传递给 setRemote()(就像问题中的片段所暗示的那样),您需要像这样设置上游:

StoredConfig config = git.getRepository().getConfig();
config.setString(CONFIG_BRANCH_SECTION, "local-branch", "remote", "remote-alias-name");
config.setString(CONFIG_BRANCH_SECTION, "local-branch", "merge", "refs/heads/name-of-branch-on-remote");
config.save();

这将导致此配置部分

[branch "local-branch"] 
remote = remote-alias-name 
merge = refs/heads/name-of-branch-on-remote

如果遥控器还没有配置(即没有部分[remote "remote-alias-name"],你也必须创建这样的部分。例如,像这样:

config.setString(CONFIG_REMOTE_SECTION, "remote-alias-name", "url", "url-of-remote");
config.setString(CONFIG_REMOTE_SECTION, "remote-alias-name", "fetch", "ref-spec");

常量在 class ConfigConstants.

中定义