This article will discuss different methods of cloning a code repository (repo) using GIT and Python. You will need to fetch the repo URL (HTTPS or SSH) first, as shown in the Figure below.
The repo in this Figure is hosted in GitHub, but you can use any other platform like GitLab or BitBucket.
Several methods and packages can be used to clone a repository. Here are some of them.
Method 1: Cloning a Repo with os or subprocess
This method does not require the installation of any new package because the two packages used here are pre-installed in Python.
The functions we use here are os.system() and subprocess.call(). These methods run the git clone command as a subshell process (just like executing the command on the terminal). Here are the examples.
os package:
1 2 3 4 5 6 7 8 9 |
import os # Path to clone the repository into # path11 folder in the current directory. to_path = "./path11" # Repo - URL. Using the SSH one here. # os.system() accepts command and options as a string os.system(f"git clone {repo_url} {to_path}") |
And for the subprocess, we use the following code.
1 2 3 4 5 6 7 |
import subprocess # Location to clone the repository into. to_path = "./path12" # subprocess.call() accepts a list of commands and options subprocess.call(["git", "clone",repo_url, to_path]) |
Method 2: Using the GitPython Package
GitPython is a popular package that provides different methods for accessing git repos. For cloning, we will use git.Repo.clone_from() function.
Note that you may need to install GitPython before using it. If you are using pip, install it with the command.
pip install gitpython
Here is the module in use.
1 2 3 4 5 6 7 8 |
# If the package is not installed, run: pip install gitpython from git import Repo # Path to clone into save_to_path = "./blog22" # Notice also you can provide the actual branch you want to clone Repo.clone_from(url=repo_url, to_path=save_to_path, branch="main") |
If to_path already exists, the module raises git.exc.GitCommandError.
Method 3: Using the pygit2 Package
The package can be installed using pip by executing this command.
pip install pygit2
If you want to clone a repository using SSH, you must install an additional dependency – libgit2.
The documentation for the package provides clear instructions on how to install pygit2 and its libgit2 for any platform.
1 2 3 4 5 6 7 8 |
from pygit2 import clone_repository # Save the cloned repo into to_path = "../path13" # HTTPS repo URL. If you want to use SSH install # the libgit2: Check https://www.pygit2.org/install.html repo_URL = "https://github.com/kipronokoech/blog1.git" clone_repository(url=repo_URL, path=to_path) |
Method 4: Another Package – dload
This package got into this list because of its potential to support parallel processing fantastically. Install the command using pip by running
pip install dload
And then clone a repo with the following snippet.
1 2 3 4 5 |
import dload repo_url = "https://github.com/kipronokoech/blog1.git" clone_into = "./path45" # path45 in the CWD dload.git_clone(git_url=repo_url, clone_dir="./path45") |
Conclusion
This article discussed different methods/packages used to clone code repositories. If you want to run the git clone command without installing additional packages, use os.system() or subprocess.call() functions as discussed in Method 1.
The rest of the methods discussed other packages that can be used for the same purpose, each with unique capabilities.