2

I am trying to pull filesfrom git using python but it's not working. Below is the code I was using:

import git 
git.cmd.Git().pull('https://github.com/User/repo','master') 

it asks for authentication and after that terminates.

Could someone please help me here? what's wrong with this code?

ZZ3111
  • 67
  • 2
  • 7

2 Answers2

3

The first step is to create a git.Repo object to represent your repository.

from git import Repo

# rorepo is a Repo instance pointing to the git-python repository.
# For all you know, the first argument to Repo is a path to the repository
# you want to work with
repo = Repo(self.rorepo.working_tree_dir)
assert not repo.bare

In the above example, the directory self.rorepo.working_tree_dir equals /Users/mtrier/Development/git-python and is my working repository which contains the .git directory. You can also initialize GitPython with a bare repository.

This is what you asked for :


 repo = git.Repo('repo_name')
 o = repo.remotes.origin
 o.pull()
Hosseinreza
  • 561
  • 7
  • 18
0

It depends on your OS but you should use a credential helper in order to cache the password (or token if you have 2FA activated) in it.

This is assuming your are trying to pull from a private repository.

On Windows, for example, that would be "manager", and you can cache your credentials with git credential fill.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Yes, but after providing credentials it should git pull but I’m unable to see any change in my folder – ZZ3111 Oct 16 '20 at 10:53
  • @ZZ3111 Then test it with git directly to see if the same issue occurs. – VonC Oct 16 '20 at 11:02