java - JGit:创建 Git 存储库并将其推送到 GitHub 时如何修复 NoRemoteRepositoryException?

标签 java jgit

使用 JGit,我想创建一个新的存储库并将其推送到 GitHub。我收到 NoRemoteRepositoryException。请帮忙。我的代码中缺少什么?

private void createNewRepository(Task task, String path) {
  File directory = new File(path);
  try {
    Git git = Git.init().setDirectory(directory).call();
    git.add().addFilepattern( "readme.txt" ).call();
    git.commit().setMessage( "Create readme file" ).call();

    RemoteAddCommand remoteAddCommand = git.remoteAdd();
    remoteAddCommand.setName("origin");
    remoteAddCommand.setUri(new URIish(task.getVcUrl()));
    remoteAddCommand.call();

    CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(task.getVcUsername(), task.getVcPassword());   
     git.push().setCredentialsProvider(credentialsProvider).
        setRemote("origin").setPushAll().setForce(true).call();
  } catch (GitAPIException ex) {
    logger.warn(ex.getLocalizedMessage(), ex);
    taskLogger.get().append(ex.getLocalizedMessage()).append("\n");
  } catch (URISyntaxException e) {
    e.printStackTrace();
  }
}

错误堆栈跟踪是

Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: 
    http://github.com/AAA/Orgtest_sd3: 
    https://github.com/AAA/Orgtest_sd3/info/refs?service=git-receive-pack not 
found
    at 
org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:520)
    at 
org.eclipse.jgit.transport.TransportHttp.openPush(TransportHttp.java:440)
    at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:160)
    at org.eclipse.jgit.transport.Transport.push(Transport.java:1344)
    at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:169)

最佳答案

无论使用 JGit 还是 native Git,您都无法从本地主机创建新的远程存储库。

您必须首先在远程主机上创建一个存储库,然后您可以从/向此存储库克隆、获取和推送。

关于java - JGit:创建 Git 存储库并将其推送到 GitHub 时如何修复 NoRemoteRepositoryException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56255068/

相关文章:

隐藏主键的Java加密性能

java - 使用 Android Studio 创建自定义 View

java - 将 Gamma 校正应用于压缩整数像素

java - JGit 中的文件提交日期

git - JGit:推送到特定分支

java - 添加到 ListModel 后如何更改 JList 中元素的 View ?

java - 没有用户名或密码的 JGit 克隆从 gerrit 失败

java - 多次尝试修复 : Malformed input or input contains unmappable chacraters 后,Eclipse 中出现连续 Git 错误

java - 将jgit添加为gradle依赖项可防止我的主类成为加载器

Java HttpURLConnection获取所有内容包括css、img等