Python way to clone a git repository

Python Programming

Question or problem about Python programming:

Is there a Python way without using a subprocess to clone a git repository? I’m up for using any sort of modules you recommend.

How to solve the problem:

Solution 1:

There is GitPython. Haven’t heard of it before and internally, it relies on having the git executables somewhere; additionally, they might have plenty of bugs. But it could be worth a try.

How to clone:

import git

(It’s not nice and I don’t know if it is the supported way to do it, but it worked.)

Solution 2:

Using GitPython will give you a good python interface to Git.

For example, after installing it (pip install gitpython), for cloning a new repository you can use clone_from function:

from git import Repo

Repo.clone_from(git_url, repo_dir)

See the GitPython Tutorial for examples on using the Repo object.

Note: GitPython requires git being installed on the system, and accessible via system’s PATH.

Solution 3:

My solution is very simple and straight forward. It doesn’t even need the manual entry of passphrase/password.

Here is my complete code:

import sys
import os

path  = "/path/to/store/your/cloned/project" 
clone = "git clone [email protected]:/your/project/name.git" 

os.system("sshpass -p your_password ssh [email protected]_localhost")
os.chdir(path) # Specifying the path where the cloned project needs to be copied
os.system(clone) # Cloning

Solution 4:

Github’s libgit2 binding, pygit2 provides a one-liner cloning a remote directory:

clone_repository(url, path, 
    bare=False, repository=None, remote=None, checkout_branch=None, callbacks=None)

Solution 5:

Here’s a way to print progress while cloning a repo with GitPython

import time
import git
from git import RemoteProgress

class CloneProgress(RemoteProgress):
    def update(self, op_code, cur_count, max_count=None, message=''):
        if message:

print('Cloning into %s' % git_root)
git.Repo.clone_from('', '/your/repo/dir', 
        branch='master', progress=CloneProgress())

Solution 6:

For python 3

First install module:

pip3 install gitpython

and later, code it 🙂

import os
from git.repo.base import Repo
Repo.clone_from("*****", "folderToSave")

I hope this helps you

Solution 7:

With Dulwich tip you should be able to do:

from dulwich.repo import Repo

This is still very basic – it copies across the objects and the refs, but it doesn’t yet create the contents of the working tree if you create a non-bare repository.

Solution 8:

Pretty simple method is to just pass the creds in the url, can be slightly suspect though – use with caution.

import os

def getRepo(repo_url, login_object):
  Clones the passed repo to my staging dir

  path_append = r"stage\repo" # Can set this as an arg 

  repo_moddedURL = 'https://' + login_object['username'] + ':' + login_object['password'] + ''
  os.system('git clone '+ repo_moddedURL)


if __name__ == '__main__':
    getRepo('', {'username': 'userName', 'password': 'passWord'})

Solution 9:

This is the sample code for gitpull and gitpush using gitpython module.

import os.path
from git import *
import git, os, shutil
# create local Repo/Folder
UPLOAD_FOLDER = "LocalPath/Folder"
if not os.path.exists(UPLOAD_FOLDER):
new_path = os.path.join(UPLOADFOLDER)
DIR_NAME = new_path
REMOTE_URL = "GitURL"  # if you already connected with server you dont need to give 
any credential
# REMOTE_URL looks "[email protected]:path of Repo"
# code for clone
class git_operation_clone():
    def __init__(self):
        self.DIR_NAME = DIR_NAME
        self.REMOTE_URL = REMOTE_URL

    def git_clone(self):

        if os.path.isdir(DIR_NAME):
        repo = git.Repo.init(DIR_NAME)
        origin = repo.create_remote('origin', REMOTE_URL)
  except Exception as e:
# code for push
class git_operation_push():
  def git_push_file(self):
        repo = Repo(DIR_NAME)
        commit_message = 'work in progress'
        # repo.index.add(u=True)
        origin = repo.remote('origin')
        print("repo push succesfully")
    except Exception as e:
if __name__ == '__main__':
   a = git_operation_push()

Solution 10:

You can use dload

import dload

pip install dload

Hope this helps!