所以我尝试使用 gitpython (https://gitpython.readthedocs.io/en/stable/intro.html https://gitpython.readthedocs.io/en/stable/intro.html)做一些相对简单但有麻烦的事情。
因此,我的 bitbucket 服务器上有一个空白的全新存储库,然后我使用 gitpython 初始化本地存储库、添加文件并成功提交。
然而,我遇到的麻烦是将这些更改推送到我拥有的全新空白远程位桶存储库。我尝试过很多事情,但我总是得到git push --set-upstream origin master
当错误返回时。但是当我导航到 repo 目录时,我可以看到它位于 master 分支上,并且当我运行时我可以看到远程 repo URLgit status
and git remote -v
.
def commit_files(url):
repo_dir = os.path.join(os.getcwd(), 'tmp')
file_name = os.path.join(repo_dir, 'Jenkinsfile')
repo = git.Repo.init(repo_dir)
open(file_name, "wb").close()
repo.index.add([file_name])
repo.index.commit("initial commit")
repo.create_remote("origin", url=url)
repo.remote("origin").push()
我发现的所有文档和 SO 帖子似乎都只是在克隆后推送到已经存在的存储库。
这也给我带来了困难,因为我尝试了和你一样的方法,显然:
创建一个新的远程 Git 存储库。
创建一个新的本地 Git 存储库(而不是克隆远程存储库)。
添加一些更改到我的本地存储库。
尝试将我的更改推送到远程存储库。
我终于找到了这次失踪失败的原因refspec https://git-scm.com/book/en/v2/Git-Internals-The-Refspec本地和远程存储库之间:
在默认情况下,由自动写入git remote add origin
命令,Git 获取下面的所有引用refs/heads/
在服务器上并将它们写入refs/remotes/origin/
本地。
可以通过添加 refspec 来解决.push()
像这样的方法:
repo.remote("origin").push('+refs/heads/*:refs/remotes/origin/*')
这应该是可以避免的,但我不知道如何避免;作者似乎更喜欢人们应用一种解决方法,而不是回答如何做到这一点的问题Github 问题#549 https://github.com/gitpython-developers/GitPython/issues/549
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)