我正在尝试自动化一个更改过程,该过程当前创建手动推送到 Git 的源代码。我正在尝试使用 GitPython 包装该代码:
from git import *
# create the local repo
repo = Repo.init("/tmp/test/repo", bare=True)
assert repo.bare == True
# check status of active branch
repo.is_dirty()
# clone the remote repo
remote_repo = repo.clone("http://user:pass@git/repo.git")
# compile source code into repository
# ...
# track untracked files
repo.untracked_files
# commit changes locally
repo.commit("commit changes")
# push changes to master
remote_repo.push()
当我尝试运行它时,我得到了
Traceback (most recent call last):
File "git_test2.py", line 33, in
repo.commit("commit changes")
BadObject: 636f6d6d6974206368616e676573
该脚本能够 pull 远程存储库,但提交失败。对此有更好的方法吗?
最佳答案
您正在使用的某些功能可能无法按您期望的方式运行。通常,Repo
方法不等同于具有相同名称的 git
子命令。
Repo.commit
does not create a commit but retrieve an existing commit.由于存储库中没有名为“commit changes”的提交,因此引发异常。Repo.clone
creates a clone of this repository在存储库目录结构内名为http:
的目录中,这很可能不是您想要的。
如果您尝试克隆远程存储库,这可以在一行中实现:
repo = Repo.clone_from("http://user:pass@git/repo.git", "/tmp/test/repo")
参见 API Reference有关如何使用 GitPython 的更多信息。
关于python - 如何使用远程分支 pull 、推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22670765/