Cloning a private repo using HTTPS with gitpython
Asked Answered
M

1

6

I am using gitpython to clone a git repository over HTTPS. If the project is a private repo, it will prompt for username and password. How do I interact with the prompt pythonically to pass username and password variables ?

from git import Repo

HTTPS_REMOTE_URL = 'https://github.com/username/private-project'
DEST_NAME = 'https-cloned-private-project'
cloned_repo = Repo.clone_from(HTTPS_REMOTE_URL, DEST_NAME)

Output of running this code:

$ python example.py
Username for 'https://github.com': example
Password for 'https://[email protected]': 

I know it's possible to include the username and password in the URL:

HTTPS_REMOTE_URL = 'https://username:[email protected]/username/private-project'

However, I have no way of knowing ahead of time if this is a private repo.

Meteorograph answered 1/4, 2016 at 14:23 Comment(0)
T
12

it works for me when using github access token instead of username and password where 2FA may be required:

HTTPS_REMOTE_URL = 'https://<access_token>:[email protected]/username/private-project'

Tarrsus answered 14/4, 2016 at 14:18 Comment(5)
This code working with Public Repo Only! and not with the Private one. Error : remote: Repository not found.Ictus
@TusharNiras actually it really works independent on the repo type. If the repo is not yours, you need to receive the invite.Junior
@ViniciusMesel is right. when github responses Error : remote: Repository not found, it's actually a status code 404 means either the repo does not exist (explicit interpretation), or this repo is not accessible by this user (implicit interpretation since GitHub should not tell you if a private repo exist or not).Tarrsus
I'm facing same problem as @TusharNiras. I'm able to clone the repo using the same command on my machine but not via my python script.Chophouse
Check this: teamdynamix.umich.edu/TDClient/47/LSAPortal/KB/…Towhead

© 2022 - 2024 — McMap. All rights reserved.