通过 JGit 添加远程



我在JGit周围玩,我可以成功地从某个存储库中删除遥控器(git remote rm origin),我该如何做一个git remote add origin http://github.com/user/repo

要删除,我执行以下操作:

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

但是没有像#setSection(String, String)这样的选择.

提前谢谢。

以这种方式管理它:

Git git = new Git(localRepository);
StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", "http://github.com/user/repo");
config.save();

有一些类可以添加新的:

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

也有RemoteSetUrlCommand

您可以使用 git24j 直接操作远程对象

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

当然,您也可以通过 git-config API 做同样的事情:

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

最新更新