【问题标题】:how to use git pull with GitPython?如何在 GitPython 中使用 git pull?
【发布时间】:2021-01-30 20:30:43
【问题描述】:

我正在尝试使用 python 从 git 中提取文件,但它不起作用。以下是我使用的代码:

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

它要求进行身份验证,然后终止。

有人可以帮我吗?这段代码有什么问题?

【问题讨论】:

    标签: python git git-pull gitpython pygit2


    【解决方案1】:

    第一步是创建一个git.Repo 对象来代表您的存储库。

    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
    
    

    在上面的示例中,目录self.rorepo.working_tree_dir 等于/Users/mtrier/Development/git-python,并且是我的工作存储库,其中包含.git 目录。您还可以使用裸存储库初始化 GitPython

    这是你要求的:

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

    【讨论】:

    • 我得到属性错误:IterableList 没有属性来源
    【解决方案2】:

    这取决于您的操作系统,但您应该使用credential helper 来缓存密码(或token,如果您有2FA activated)。

    这是假设您正在尝试从私有存储库中提取。

    例如,在 Windows 上,这将是“manager”,您可以使用 git credential fill 缓存您的凭据。

    【讨论】:

    • 是的,但是在提供凭据后它应该 git pull 但我无法在我的文件夹中看到任何更改
    • @ZZ3111 然后直接用git测试一下,看看会不会出现同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-03
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2020-03-25
    • 2011-09-15
    相关资源
    最近更新 更多