Python way to clone a git repository
- Patch Django Site Package From a Pull Request Using Pip
- How do I pull a specifc commit in SmartGit and overwrite the local file so that I can open the previous commit in PyCharm?
- Convert python 2 code to 3 in PyCharm
- Using python sh module with git
- Properly specifying path for git pull from my local development machine
- Compare old and new file pushed to git repository with (preferably) python hook
5 Solutions collect form web for “Python way to clone a git repository”
There is http://gitorious.org/git-python/ . 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().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.)
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)
Note: GitPython requires git being installed on the system, and accessible via system’s PATH.
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)
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.
My solution is very simple and straight forward. It doesn’t even need the manual entry of paraphrase/password.
Here is my complete code:
import os import sys path = "/path/to/store/your/cloned/project" clone = "git clone email@example.com:/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 has to be copied os.system(clone) # Cloning print "\n CLONED SUCCESSFULLY.! \n"