145

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.

Mike
  • 7,769
  • 13
  • 57
  • 81
  • 3
    gitpy, i guess it would be called – SilentGhost Mar 18 '10 at 18:58
  • @SilentGhost: you mean *this* gitpy? http://github.com/vmalloc/gitpy from http://www.ryaari.com/blog/?p=9 – VonC Mar 18 '10 at 19:01
  • Looks like there's GitPython (http://pypi.python.org/pypi/GitPython, http://gitorious.org/git-python) which I don't think has a clone method, but I'll bet you could add one... internally it's going to have to call `git clone` anyway though. – Cascabel Mar 18 '10 at 19:04
  • 1
    [Dulwich][1] is a pure-Python implementation of Git that does not fork at all. Be aware that it's still under development, so it may be buggy. [1]:http://samba.org/~jelmer/dulwich/ – Mark Lodato Mar 28 '10 at 03:50

12 Answers12

212

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.

Amir Ali Akbari
  • 5,973
  • 5
  • 35
  • 47
  • 3
    How to handle authentication if it has to run in an automated way ? – SunilS Jan 11 '20 at 10:49
  • 3
    You can provide the authentication in the git_url, depending on from where you clone the repo, you may need to put the username and password/pat in there. See here for [Github](https://stackoverflow.com/questions/10054318/how-do-i-provide-a-username-and-password-when-running-git-clone-gitremote-git) – LemurPwned Jan 17 '20 at 14:51
  • 2
    Could we get an example of `git_url` and `repo_dir`? – Sam May 14 '22 at 09:20
85

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
git.Git("/your/directory/to/clone").clone("git://gitorious.org/git-python/mainline.git")

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

breandan
  • 1,965
  • 26
  • 45
Debilski
  • 66,976
  • 12
  • 110
  • 133
  • It does. But it is a little convoluted. – Debilski Mar 18 '10 at 19:07
  • 1
    Oh, my bad, I missed that possibility. Mike, just remember, internally this is just calling the git executable anyway; it's just managing it a little for you. – Cascabel Mar 18 '10 at 19:19
  • I looked at gitorious.. just overlooked the clone option since its not documented at all.. but I expected whatever i used to to some sort of process command.. this works thanks! – Mike Mar 18 '10 at 19:29
  • This module was really helpful, thank you. Can you help me how to pull the master branch of already cloned repo using this module – The Gr8 Adakron Mar 09 '19 at 13:41
  • 3
    How to handle authentication if it has to run in an automated way ? – SunilS Jan 11 '20 at 10:48
  • Yes, i used gitpython. The drawback is that it is not pure API. It is dependent on git binary ( mandatory) to be present. For Authentication, i used personal access token. – SunilS Apr 13 '20 at 03:05
30

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 gitolite@<server_ip>:/your/project/name.git" 

os.system("sshpass -p your_password ssh user_name@your_localhost")
os.chdir(path) # Specifying the path where the cloned project needs to be copied
os.system(clone) # Cloning
Rob Heiser
  • 2,792
  • 1
  • 21
  • 28
Manje
  • 411
  • 4
  • 7
  • 2
    Works great, however if you use other relative paths in your project, you may want to remember the true working directory ``os.getcwd()`` before changing it with ``os.chdir(...)`` and reset it back afterwards. – Maximosaic Oct 24 '18 at 12:03
  • 1
    @Maximosaic this can be avoided by using `git clone `. No need to use `chdir` – Lahiru Chandima Dec 01 '20 at 07:00
  • work only on linux and mac. not work on windows – matan h Dec 22 '20 at 15:05
  • In addition to the other potential caveats already mentioned, if you want to run a shell command (in that case git cli) better go if favour of subprocess over os as os.system will be depreceted, cheers – theraulpareja Feb 01 '21 at 17:38
18

For python 3

First install module:

pip3 install gitpython

and later, code it :)

from git.repo.base import Repo
Repo.clone_from("https://github.com/*****", "folderToSave")

I hope this helps you

Boern
  • 7,233
  • 5
  • 55
  • 86
Mike Brian Olivera
  • 1,414
  • 1
  • 16
  • 21
13

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)
chiffa
  • 2,026
  • 3
  • 26
  • 41
  • Would this require installing `libgit2` separately, or does it come bundled together with `pygit2`? – norok2 Aug 05 '22 at 14:55
13

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(message)

print('Cloning into %s' % git_root)
git.Repo.clone_from('https://github.com/your-repo', '/your/repo/dir', 
        branch='master', progress=CloneProgress())
Adam Parkin
  • 17,891
  • 17
  • 66
  • 87
crizCraig
  • 8,487
  • 6
  • 54
  • 53
  • 1
    Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From [review](https://stackoverflow.com/review). – Trenton McKinney Sep 24 '19 at 00:51
5

You can use dload

import dload
dload.git_clone("https://github.com/some_repo.git")

pip install dload
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
4

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 
  os.chdir(path_append)

  repo_moddedURL = 'https://' + login_object['username'] + ':' + login_object['password'] + '@github.com/UserName/RepoName.git'
  os.system('git clone '+ repo_moddedURL)

  print('Cloned!')


if __name__ == '__main__':
    getRepo('https://github.com/UserName/RepoYouWant.git', {'username': 'userName', 'password': 'passWord'})
haku-kiro
  • 66
  • 3
4

With Dulwich tip you should be able to do:

from dulwich.repo import Repo
Repo("/path/to/source").clone("/path/to/target")

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.

jelmer
  • 2,405
  • 14
  • 27
1

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):
  os.makedirs(UPLOAD_FOLDER)
  print(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 "git@github.com:path of Repo"
# code for clone
class git_operation_clone():
  try:
    def __init__(self):
        self.DIR_NAME = DIR_NAME
        self.REMOTE_URL = REMOTE_URL

    def git_clone(self):

        if os.path.isdir(DIR_NAME):
            shutil.rmtree(DIR_NAME)
        os.mkdir(DIR_NAME)
        repo = git.Repo.init(DIR_NAME)
        origin = repo.create_remote('origin', REMOTE_URL)
        origin.fetch()
        origin.pull(origin.refs[0].remote_head)
  except Exception as e:
      print(str(e))
# code for push
class git_operation_push():
  def git_push_file(self):
    try:
        repo = Repo(DIR_NAME)
        commit_message = 'work in progress'
        # repo.index.add(u=True)
        repo.git.add('--all')
        repo.index.commit(commit_message)
        origin = repo.remote('origin')
        origin.push('master')
        repo.git.add(update=True)
        print("repo push succesfully")
    except Exception as e:
        print(str(e))
if __name__ == '__main__':
   a = git_operation_push()
   git_operation_push.git_push_file('')
   git_operation_clone()
   git_operation_clone.git_clone('')
v.k
  • 141
  • 6
0

The easiest way to clone a repo on windows is:

  1. pip install clone
  2. clone [REPO] [USERNAME]

Example: clone Wifi-Brute Cyber-Dioxide

You can execute it via shell command

import os os.system("pip install clone") os.system("clone SSH-Brute Cyber-Dioxide")

-1

We can use simple solution without any library.

#!/usr/bin/python
import os

destination_path  = "destination/path/where/project/to/be/cloned"
clone_command = "git clone https://your.git.servername/git-folder/repo-name.git" 

clone_with_path = clone_command  +" "+ destination_path
os.system(clone_with_path)

Perk: It will create a destination folder if it doesn't exist.

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70