JGit - pushing a branch and adding upstream (-u option)

In JGit , I am looking for a way to push a branch and add an upstream link (tracking).

This is the -u or --set-upstream option in the command.

I do not see a method in the PushCommand class that allows this.

Please, could you tell me how can I do this?

 PushCommand pushCommand = git.push() .setRemote(remoteAlias) .setRefSpecs(spec); 
+7
java jgit
source share
1 answer

JGit PushCommand does not offer this feature (yet), but you can change the repository configuration, for example --set-upstream .

If you pass the remote alias to setRemote() (as the fragment from the question shows), you need to set the upstream as follows:

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

As a result of this configuration section

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

If the remote has not yet been configured (ie there is no [remote "remote-alias-name"] section, you also need to create such a section. For example, for example:

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

Constants are defined in the ConfigConstants class.

+6
source share

All Articles