Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add remote via JGit

Tags:

java

git

jgit

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.

like image 664
caarlos0 Avatar asked Oct 09 '12 12:10

caarlos0


People also ask

What is JGit used for?

JGit is a lightweight, pure Java library implementation of the Git version control system – including repository access routines, network protocols, and core version control algorithms. JGit is a relatively full-featured implementation of Git written in Java and is widely used in the Java community.


2 Answers

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

And aparently it works like a boss.

like image 196
caarlos0 Avatar answered Sep 20 '22 16:09

caarlos0


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.

like image 21
Daniel Flower Avatar answered Sep 18 '22 16:09

Daniel Flower