简体   繁体   English

通过 JGit 添加远程

[英]Add remote via 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 ?我在玩 JGit,我可以成功地从某个存储库( git remote rm origin )中删除一个遥控git remote rm origin ,我该如何做一个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) .但是没有像#setSection(String, String)这样的选项。

Thanks in advance.提前致谢。

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.显然它像老板一样工作。

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.还有一个RemoteSetUrlCommand

You can direct manipulate remote object with git24j您可以使用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:当然,你也可以通过 git-config API 做同样的事情:

Config cfg = Config.openOndisk("my-git.config");
cfg.setString("remote.url", "http://github.com/user/repo");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM