Cloning a private Github repo using a script
Asked Answered
M

4

6

How to clone a private repository from Github using python?

I found some good information about git and python, but I started learning python few days back.

Marrowbone answered 29/8, 2014 at 11:3 Comment(1)
What prohibits you from invoking "git clone ..."?Ransome
S
3

There is a library, libgit2, which enables git to be used as a shared library more helpful to your cause is the python binding's pygit.

To answer your question using pygit to clone a repo:

>>> from pygit2 import clone_repository
>>> repo_url = 'git://github.com/libgit2/pygit2.git'
>>> repo_path = '/path/to/create/repository'
>>> repo = clone_repository(repo_url, repo_path) # Clones a non-bare repository
>>> repo = clone_repository(repo_url, repo_path, bare=True) # Clones a bare repository

You can view the repository based docs here

Saval answered 29/8, 2014 at 11:7 Comment(3)
to make it clear, i want to make a python script that will clone a github private repository to my pc, i just want to find how to do it and a guide to itMarrowbone
@user3914163 In this case the simplest way is to simply call the git binary from python, as Messa wrote.Saval
How do you authenticate with that? The question was about a private repo, right?Fluctuant
B
5

Just run the git command with subprocess.check_call:

import subprocess
subprocess.check_call(["git", "clone", ...])
Brushwork answered 29/8, 2014 at 11:12 Comment(0)
S
3

There is a library, libgit2, which enables git to be used as a shared library more helpful to your cause is the python binding's pygit.

To answer your question using pygit to clone a repo:

>>> from pygit2 import clone_repository
>>> repo_url = 'git://github.com/libgit2/pygit2.git'
>>> repo_path = '/path/to/create/repository'
>>> repo = clone_repository(repo_url, repo_path) # Clones a non-bare repository
>>> repo = clone_repository(repo_url, repo_path, bare=True) # Clones a bare repository

You can view the repository based docs here

Saval answered 29/8, 2014 at 11:7 Comment(3)
to make it clear, i want to make a python script that will clone a github private repository to my pc, i just want to find how to do it and a guide to itMarrowbone
@user3914163 In this case the simplest way is to simply call the git binary from python, as Messa wrote.Saval
How do you authenticate with that? The question was about a private repo, right?Fluctuant
Q
2

Here are my two cents since there's no answer yet to the repo being private. The way I usually do it is I create a special SSH key pair for the script and upload the public one to GitHub (or whatever hosting you're using).

You can have the script use the private key by running:

GIT_SSH_COMMAND='ssh -i private_key_file' git clone [email protected]:user/repo.git
Quatre answered 1/4, 2020 at 3:16 Comment(0)
T
0
import pygit2
repo_url = 'git://github.com/libgit2/pygit2.git'
repo_path = '/path/to/create/repository'
callbacks = pygit2.RemoteCallbacks(pygit2.UserPass("<your-personal-token>", 'x-oauth-basic'))
repo = pygit2.clone_repository(repo_url, repo_path, callbacks=callbacks)
Tetanic answered 28/7, 2021 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.