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

JGit: How to fix NoRemoteRepositoryException while creating Git Repository and pushing it to GitHub?

我想使用 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 还是本地 Git。

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