32

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 its origin (origin.rename). How can I achieve this?

5 Answers 5

60

I managed this by getting the repo name directly:

 repo = git.Repo('repo_path')
 o = repo.remotes.origin
 o.pull()
Sign up to request clarification or add additional context in comments.

7 Comments

git.Repo(repo_dir).remotes[remote].pull() if your remote is a string
git.Repo(repo_dir).remotes.origin.pull(options), where, for example options='--tags'
repo = git.Repo(localpath_to_repo_dir) repo.remotes.origin.pull(branch_name) if you want to pull from a branch by name
How to force pull using this method ?
In case somebody is interested, that command displayed a lot of "trash"(it wasn't useful for me at least) on the screen. The way of getting around it is to assign the result to _. Something like this: _ = Repo(folder_name).remotes.origin.pull()
|
11

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.'

Comments

11

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.

1 Comment

Image
Thanks! This also works if you want to pull directly from a URL
5

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')

1 Comment

Image
That change you reffer to has been made over 15 years ago..
0

Since it has been asked

How to force pull using this method ?

 repo = git.Repo('repo_path')
 repo.git.pull('--force')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.