如何解决使用PyGithub和Python将本地git存储库推送到用户的远程git存储库
任务是
CLI命令运行正常,但是我需要使用python 进行操作,当我运行此代码时,将打开一个github登录对话框,输入凭据,然后推送有效,但没有任何帮助在远程存储库上看到的内容,我们可以不执行任何操作吗?
我也尝试过使用子进程模块,它不起作用。我检查了所有其他stackoverflow解决方案,但没有找到有效的解决方案。我需要一个新的解决方案或对此aap进行更正。谢谢。
import git
git.Repo.clone_from('https://github.com/kristej/Uniform-Database-Management.git','Uniforms')
repo = git.Repo('Uniforms')
target_repo = "https://github.com/kristej/Jojorabit.git"
# List remotes
# Reference a remote by its name as part of the object
print(f'Remote name: {repo.remotes.origin.name}')
print(f'Remote URL: {repo.remotes.origin.url}')
# Delete a default remote if already present
if repo.remotes.origin.url != target_repo:
repo.delete_remote(repo.remotes.origin.name)
# Create a new remote
try:
remote = repo.create_remote('origin',url=target_repo)
except git.exc.GitCommandError as error:
print(f'Error creating remote: {error}')
# Reference a remote by its name as part of the object
print(f'Remote name: {repo.remotes.origin.name}')
print(f'Remote URL: {repo.remotes.origin.url}')
#Push changes
print(repo.git.push("origin","HEAD:refs/for/master"))
解决方法
我们什么也不能推?
是的。如果没有要推送的内容,推送将成功。
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit,working tree clean
$ git push
Everything up-to-date
但是您要推送到一个奇怪的位置。 HEAD:refs/for/master
说将HEAD
推到refs/for/master
。 refs/for/master
不是问题。我认为您的意思是refs/heads/master
。
但是除非您确实需要,否则您不应该指定目的地,HEAD
不是master
吗?让Git找出源(当前签出的分支)及其目的地(当前分支的上游,或基于push.default
的最佳猜测)。
您只需完成大量工作即可更改origin
,而无需再次指定。再次强调,根据本地分支配置,让push推算到哪里更安全。
print(repo.git.push())
您也无需仅更改一次即可更改origin
。您可以改为添加一个新的遥控器并推送到该遥控器。
repo.create_remote('other_remote',url=target_repo)
repo.git.push("other_remote")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。