Skip to content

Instantly share code, notes, and snippets.

Created Sep 1, 2021
What would you like to do?
///usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS org.eclipse.jgit:org.eclipse.jgit:
//DEPS org.slf4j:slf4j-nop:1.7.31
package me.porcelli.jgit;
import com.jcraft.jsch.*;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.SshTransport;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.util.FS;
public class jbang {
public static void main(String... args) throws Exception {
CloneCommand cloneCommand = Git.cloneRepository();
cloneCommand.setURI( args[0] );
cloneCommand.setTransportConfigCallback(transport -> {
final SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(new JschConfigSessionFactory() {
protected void configure(OpenSshConfig.Host host, Session session) {}
protected JSch createDefaultJSch(FS fs) throws JSchException {
JSch defaultJSch = super.createDefaultJSch( fs );
defaultJSch.addIdentity( "/path/to/private_key" );
return defaultJSch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment