Search code examples
pythongitgitpython

GitPython Clone repository error


I want to clone git repository with parameters (--recursive, -b <branch>) but I get the following error.

Traceback (most recent call last):
  File "./git-clone.py", line 15, in <module>
    r = git.Repo.clone(repo_dir, b=branch, recursive=git_url)
TypeError: unbound method clone() must be called with Repo instance as first argument (got str instance instead)

Here is my code:

#!/usr/bin/env python

import git
import os
import shutil


git_url = "<url>..."
repo_dir = "/home_local/user/git-repository"
branch = "branch"

if os.path.exists(repo_dir):
    shutil.rmtree(repo_dir)

r = git.Repo.clone(repo_dir, b=branch, recursive=git_url)

If I replace git.Repo.clone with git.Repo.clone_from its working fine but this command not accept my parameters.


Solution

  • try:

    r = git.Repo.clone_from(git_url, repo_dir, branch=branch, recursive=True)
    

    The first argument, is where you cloning from (the remote repository). The second argument is where you want to store the clone. All other arguments are passed on to git-clone command. eg --branch="branch" and --recursive. You should probably stick to the long argument names rather than the abbreviations. Since the recursive flag is either present or not, it's values can only be True or False.