Search code examples
gitclonegit-clonejgit

How to do a "git clone --mirror" in JGit?


Reading the documentation it seems like there is no explicit method to do a git clone --mirror in JGit.

In case of git push there is a workaround. I was wondering if there is any for git clone as well. Any idea?


Solution

  • My understanding is, that when using --mirror, all refs are copied as-is. To simulate this behaviour with JGit, initialize a bare repository and configure the remote repository.

    Now use the FetchCommand to transfer all refs from the remote repository. Make sure to specify the ref-specs to include all relevant refs like heads, tags, notes, etc.

    Git git = Git.init().setBare(true).setGitDir(...).call()
    git.remoteAdd().setName("origin").setUri(new URIish("URL/to/mirror/source")).call();
    git.fetch().setRemote("origin").setRefSpecs("+refs/*:refs/*").call();