20

I playing around JGit, I could successfully remove a remote from some repository (git remote rm origin), how can I do a git remote add origin http://github.com/user/repo ?

To remove I do the following:

StoredConfig config = git.getRepository().getConfig();
config.unsetSection("remote", "origin");
config.save();

But there's no a option like #setSection(String, String).

Thanks in advance.

caarlos0
  • 20,020
  • 27
  • 85
  • 160

3 Answers3

35

Managed it to work that way:

Git git = new Git(localRepository);
StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", "http://github.com/user/repo");
config.save();
xdevs23
  • 3,824
  • 3
  • 20
  • 33
caarlos0
  • 20,020
  • 27
  • 85
  • 160
1

There are classes to add new ones:

    RemoteAddCommand remoteAddCommand = git.remoteAdd();
    remoteAddCommand.setName("origin");
    remoteAddCommand.setUri(new URIish("http://github.com/user/repo"));
    remoteAddCommand.call();

There is a RemoteSetUrlCommand too.

Daniel Flower
  • 687
  • 7
  • 13
0

You can direct manipulate remote object with git24j

Repository repo = Repository.open("your-repository");
Remote upstream = Remote.create(repo, "upstream", URI.create("http://github.com/user/repo"));

and of cause you can also do the same thing through git-config APIs:

Config cfg = Config.openOndisk("my-git.config");
cfg.setString("remote.url", "http://github.com/user/repo");
Shijing Lv
  • 6,286
  • 1
  • 20
  • 12