gpt4 book ai didi

使用用户名和密码的 gitpython git 身份验证

转载 作者:太空狗 更新时间:2023-10-29 12:48:57 28 4
gpt4 key购买 nike

我正在使用 GitPython,但没有找到使用用户名和密码推送到存储库的方法。任何人都可以给我一个工作示例或给我一些关于如何去做的指示吗?我需要做的是:将文件添加到存储库,使用提供的用户名和密码推送它。

最佳答案

什么对我有效(与 GitHub 合作,自托管 BitBucket,很可能也适用于 GitLab)。

先决条件

请注意,尽管名称如此,password 是由 GitHub 生成的您的访问 token ,而不是您的 GitHub 密码。

from git import Repo

full_local_path = "/path/to/repo/"
username = "your-username"
password = "your-password"
remote = f"https://{username}:{password}@github.com/some-account/some-repo.git"

克隆存储库

这会将您的凭据存储在 .git/config 中,您以后将不需要它们。

Repo.clone_from(remote, full_local_path)

提交更改

repo = Repo(full_local_path)
repo.git.add("rel/path/to/dir/with/changes/")
repo.index.commit("Some commit message")

推送更改

如上所述,您不需要凭据,因为它们已存储在 .git/config 中。

repo = Repo(full_local_path)
origin = repo.remote(name="origin")
origin.push()

关于使用用户名和密码的 gitpython git 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44784828/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com