Specifying ssh key for jgit

I’m wondering how I can use jgit to connect to github using a specified ssh key file (i.e. one not in ~/.ssh/).

Unfortunately, I’m not sure how to use JschConfigSessionFactory properly. I’ve tried creating a setup just like the one in this article: Using Keys with JGit to Access a Git Repository Securely

  • Does no conflict of git merge mean everything OK?
  • What is syntax for ignoring a directory in Git?
  • Workflow best practice with git and github?
  • Git: how to roll back to last push/commit
  • Can a developer irreversibly damage git repo?
  • git-diffall and git difftool --dir-diff not working with Araxis Merge
  • I call git using git.push().setRemote(remotePath).call(); However, I get this error (specific repository omitted from log):

    org.eclipse.jgit.api.errors.TransportException: https://github.com/user/repo: not authorized
        at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:160)
        at gitio.GitInterface.pushToRemote(GitInterface.java:145)
        at engine.GitInterfaceTester.main(GitInterfaceTester.java:25)
    Caused by: org.eclipse.jgit.errors.TransportException: https://github.com/user/repo: not authorized
        at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:479)
        at org.eclipse.jgit.transport.TransportHttp.openPush(TransportHttp.java:396)
        at org.eclipse.jgit.transport.PushProcess.execute(PushProcess.java:154)
        at org.eclipse.jgit.transport.Transport.push(Transport.java:1173)
        at org.eclipse.jgit.api.PushCommand.call(PushCommand.java:156)
        ... 2 more

    I’ve noticed that the custom overriden methods in JschConfigSessionFactory aren’t ever actually invoked. This is almost certainly the cause of the problem… but don’t know why they’re not invoked; I pass the custom JschConfigSessionFactory to SshSessionFactory using SshSessionFactory.setInstance(sessionFactory);

    Does anybody know what I’m doing wrong?

  • sublime text option “-n -w” (for git) always open my previous tabs
  • What of Eclipse project .metadata can I safely ignore in Git/Mercurial?
  • Git: How does git remember the index for each branch?
  • Why don't git colors show up in iTerm2
  • Is it possible to do a git-rebase after doing a git-commit
  • GitZilla's update hook error?
  • One Solution collect form web for “Specifying ssh key for jgit”

    GitHub uses password authentication for https URLs and public-key authentication for SSH URLs (see https://gist.github.com/grawity/4392747).

    The SshSessionFactory is only consulted for SSH URLs like git@github.com:user/repo.git. To authenticate for an https URL you can use the CredentialsProvider. The base class for commands that may establish connections is TransportCommand and has a setCredentialsProvider() method.
    If you equip the PushCommand with a CredentialsProvider that supplies user name and password, you should be able to successfully establish a connection.

    For further details about authenticating with JGit you may want to read this article: http://www.codeaffine.com/2014/12/09/jgit-authentication/

    Git Baby is a git and github fan, let's start git clone.