How to clone from specific branch from Git using Gitpython

Antony picture Antony · Apr 20, 2017 · Viewed 12k times · Source

I tried to clone a repository from git using GitPython in python function. I used GitPython library for cloning from git in my python function and my code snippet as follows:

from git import Repo

Repo.clone_from('http://user:[email protected]/user/project.git', /home/antro/Project/')

It clones from master branch. How do I clone from other branch using GitPython or any other library is available to clone from individual branches? Please let me know.

I am aware of clone by mentioning branch in commandline using

git clone -b branch http://github.com/user/project.git

Answer

toanant picture toanant · Apr 20, 2017

just pass the branch name parameter, e.g. :-

repo = Repo.clone_from(
    'http://user:[email protected]/user/project.git',
    '/home/antro/Project/',
    branch='master'
)

see here for more info