当存储库已经存在时调用 git.init()

Calling git.init() when repository already exists

我正在使用以下代码片段来初始化 git 存储库:

try (Git git = Git.init().setDirectory(gitFilename).call()) {
    log.info("Created repository: " + git.getRepository().getDirectory());
} catch (GitAPIException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

当存储库已经创建并且我对其调用 git.init() 时会发生什么?

谢谢!安娜

这似乎对我有用:

//initialize git repository
        String gitDirectory = getGitRepoDirectory();
        File gitFilename = new File(gitDirectory);
        try (Git git = Git.open(gitFilename)){
            log.info("Git repo " + gitDirectory + " exists!");;
        } catch (RepositoryNotFoundException e) {
            log.info("Initialising " + gitDirectory + " as a git repo for backup purposes");
            try {
                Git git = Git.init().setDirectory(gitFilename).call();

                } catch (GitAPIException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }