简体   繁体   中英

Cloning a private Github repo using a script

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.

Just run the git command with subprocess.check_call :

import subprocess
subprocess.check_call(["git", "clone", ...])

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

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 git@github.com:user/repo.git
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM