Search code examples
githubgitpython

How to pull a certain branch from a upstream repository


Assume I have a local git clone called GitPython. I'm able to commit and push using gitpython:

repo = Repo(D:\Dev\Gitpython)
print(repo.git.add("."))
print(repo.git.commit(m='my commit message'))
print(repo.git.push())

However, how can I pull from the upstream repository using gitpython? I tried to create an remote object by using Repo.create_remote(), but it gives me an error since the remote is already exist.


Solution

  • Since the connection already exist your should be able to pull.

    repo = git.Repo('repo_name')
    o = repo.remotes.origin
    o.pull()
    
    
    o = repo.remotes.origin
    o.fetch('branch_name')