1

I am trying to pull a repo from my Github account using GitPython. This is after

(1) I performed a git clone already from the command line.

(2) Generated new SSH keys using

ssh-keygen -t rsa -b 4096

(3) Setup the contents of the .pub file from #2 above in Github as a new SSH key.

I still get prompted to enter my Github username and password. Why is that ?

Here is the config in my .git folder. Note the http:// in the url instead of https://

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = http://github.com/githubuser/myrepo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

Here is my code snippet

import git, os

DIR_NAME = 'path/to/dir/with/gitfolder'
git_ssh_identity_file = os.path.expanduser('/path/to/rsa/key')
git_ssh_cmd = 'ssh -i %s' % git_ssh_identity_file

with git.Git().custom_environment(GIT_SSH_COMMAND=git_ssh_cmd):

    repo = git.Repo.init(DIR_NAME)
    origin = repo.remote()
    refs = origin.refs

    for ref in refs:
        if ref.remote_head == "master":
            origin.pull(ref.remote_head)
DancingJohn
  • 557
  • 1
  • 9
  • 17

1 Answers1

2

Note the http:// in the url instead of https://4

As long as you are using an http(s) url, ssh won't come into play at all for authentication.

You need an ssh url:

git@github.com:USERNAME/OTHERREPOSITORY.git

An http url would still rely on the username/password authentication scheme.
Only an ssh url would use your public/private keys.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Changing to ssh url worked, however now I seem to get this error : cmdline: git pull -v origin master stderr: 'fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.' I can get over this by running these 2 in sequence - (1)ssh-agent -s (2)ssh-add ~/.ssh/id_rsa. However this doesn't seem to stick around e.g. few mins later (or new session) it complains about access permissions again until I run the same 2 commands. – DancingJohn May 14 '17 at 15:49
  • @DancingJohn Is your private ssk key protected by a passphrase? You really don't need a passphrase for that: you could generate one without passphrase. – VonC May 14 '17 at 15:53
  • @DancingJohn But you need to have a private key protected by a passphrase, then https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#adding-your-ssh-key-to-the-ssh-agent is the reference. Make sure your agent is automatically launched, as described in https://help.github.com/articles/working-with-ssh-key-passphrases/ – VonC May 14 '17 at 15:54
  • No passphrase. Though my actual filename is github_id_rsa instead id_rsa. Hope that's not the issue. – DancingJohn May 14 '17 at 16:48
  • @DancingJohn Then you don't need ssh_agent. But if your ssh key isn't named id_rsa, you will need to .ssh/config file: see for instance http://stackoverflow.com/a/22769324/6309 – VonC May 14 '17 at 17:02
  • Got it. I just renamed to id_rsa and it works well. Dom't feel like there's a need of maintaining multiple SSH keys. Thanks for all the help. – DancingJohn May 14 '17 at 19:10