How can I pull a remote repository with GitPython?
Solution 1
I managed this by getting the repo name directly:
repo = git.Repo('repo_path')
o = repo.remotes.origin
o.pull()
Solution 2
Hope you are looking for this :
import git
g = git.Git('git-repo')
g.pull('origin','branch-name')
Pulls latest commits for the given repository and branch.
Solution 3
As the accepted answer says it's possible to use repo.remotes.origin.pull()
, but the drawback is that it hides the real error messages into it's own generic errors. For example when DNS resolution doesn't work, then repo.remotes.origin.pull()
shows the following error message:
git.exc.GitCommandError: 'Error when fetching: fatal: Could not read from remote repository.
' returned with exit code 2
On the other hand using git commands with GitPython like repo.git.pull()
shows the real error:
git.exc.GitCommandError: 'git pull' returned with exit code 1
stderr: 'ssh: Could not resolve hostname github.com: Name or service not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.'
Solution 4
git.Git module from Akhil Singhal s' answer above still works, but has been renamed to git.cmd.Git, e.g.:
import git
# pull from remote origin to the current working dir:
git.cmd.Git().pull('https://github.com/User/repo','master')
Related videos on Youtube
![Uuid](https://i.stack.imgur.com/Hj1mQ.jpg?s=256&g=1)
Comments
-
Uuid almost 2 years
I am trying to find the way to pull a git repository using gitPython. So far this is what I have taken from the official docs here.
test_remote = repo.create_remote('test', 'git@server:repo.git') repo.delete_remote(test_remote) # create and delete remotes origin = repo.remotes.origin # get default remote by name origin.refs # local remote references o = origin.rename('new_origin') # rename remotes o.fetch() # fetch, pull and push from and to the remote o.pull() o.push()
The fact is that I want to access the repo.remotes.origin to do a pull withouth renaming it's origin (origin.rename) How can I achieve this? Thanks.
-
Paul Tobias over 6 yearsThe
repo_name
here is not actually the name of the repo, but the filesystem path to the base of the git repository. -
crizCraig over 5 years
git.Repo(repo_dir).remotes[remote].pull()
if your remote is a string -
Gulzar almost 5 years
git.Repo(repo_dir).remotes.origin.pull(options)
, where, for exampleoptions='--tags'
-
HolyM over 4 yearsWork for me :) Thanks!
-
Daniel Lavedonio de Lima almost 4 yearsThanks! This also works if you want to pull directly from a URL
-
otaku almost 4 years
repo = git.Repo(localpath_to_repo_dir) repo.remotes.origin.pull(branch_name)
if you want to pull from a branch by name -
Mike over 3 yearsHow to force pull using this method ?