Java 在github中添加head/ref/master路径的位置

Java 在github中添加head/ref/master路径的位置,java,github,egit,jgit,Java,Github,Egit,Jgit,我正在使用jgit。我是新手,我能够从github克隆代码,但当我尝试用java推送代码时,它给了我错误 以下是代码: 公共类PullFromRemoteRepository{ private static final String REMOTE_URL = "https://github.com/raghav1/local.git"; public static void main(String[] args) throws IOException, InvalidRemoteExcepti

我正在使用jgit。我是新手,我能够从github克隆代码,但当我尝试用java推送代码时,它给了我错误

以下是代码: 公共类PullFromRemoteRepository{

private static final String REMOTE_URL = "https://github.com/raghav1/local.git";

public static void main(String[] args) throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("raghav345", "");
    localPath.delete();

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    Git.cloneRepository()
            .setURI(REMOTE_URL)
            .setDirectory(localPath)
            .call();

    // now open the created repository
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(localPath)
            .readEnvironment() // scan environment GIT_* variables
            .findGitDir() // scan up the file system tree
            .build();

    Git git = new Git(repository);
    git.pull()
            .call();

    System.out.println("Pulled from remote repository to local repository at " + repository.getDirectory());

    repository.close();
}
}

错误如下:

Git git = Git.cloneRepository().setURI( REMOTE_URL ).setDirectory( new File( "local/path/to/repo" ) ).call();
File gitDir = git.getRepository().getDirectory();
git.close();
// ...
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir( gitDir ).setMustExist( true ).build();
线程“main”org.eclipse.jgit.api.errors.NoHeadException中出现异常:当前不支持不带HEAD的拉入存储库 在org.eclipse.jgit.api.PullCommand.call上(PullCommand.java:170) 位于org.dstadler.jgit.unfinished.PullFromRemoteRepository.main(PullFromRemoteRepository.java:61)

有谁能帮我把头路定在哪里


谢谢

注释“立即打开已创建的存储库”后的代码实际上并没有打开克隆的存储库。因此,
pull
命令作用于空存储库

重新使用从
CloneCommand
返回的
Git
实例,或者将代码更改为如下内容:

Git git = Git.cloneRepository().setURI( REMOTE_URL ).setDirectory( new File( "local/path/to/repo" ) ).call();
File gitDir = git.getRepository().getDirectory();
git.close();
// ...
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir( gitDir ).setMustExist( true ).build();

当我使用你的建议时。它在线程“main”java.lang中给了我这个错误异常。错误:未解决的编译问题:CloneCommand类型中的方法setDirectory(文件)不适用于参数(字符串),正如异常消息所说,尽管存在编译错误,您仍在运行代码段。将“local/path/to/repo”替换为指向克隆存储库应该位于的目录的文件-与问题片段中的操作相同。