如何使用远程分支拉取、推送

问题描述 投票:0回答:2

我正在尝试自动化一个更改过程,该过程当前创建手动推送到 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()

当我尝试运行这个时,我得到了

回溯(最近一次调用最后一次):

文件“git_test2.py”,第 33 行,位于

repo.commit(“提交更改”)

坏对象:636f6d6d6974206368616e676573

该脚本能够拉取远程存储库,但提交失败。有更好的方法吗?

python git git-push git-pull gitpython
2个回答
4
投票

您正在使用的某些功能可能无法按您期望的方式工作。一般来说,

Repo
方法相当于同名的
git
子命令。

如果您尝试克隆远程存储库,只需一行即可实现:

repo = Repo.clone_from("http://user:pass@git/repo.git", "/tmp/test/repo")

有关如何使用 GitPython 的更多信息,请参阅 API 参考


1
投票

您无法针对裸存储库进行提交。您只能推/拉它们。同时考虑一下如何在本地执行此操作。尝试克隆一个裸仓库并执行操作,它们不会起作用。

我对 pythonic git 绑定不是很熟悉,但我想象你需要克隆一个工作存储库,可选择签出给定的分支而不是主分支,完成你的工作,针对这些东西调用 git add ,然后提交.

此外,repo.untracked_files 是一个无操作,它只是列出它们,而不是添加它们。

老实说,看起来你盲目地从 https://pythonhosted.org/GitPython/0.3.1/tutorial.html 复制粘贴,而没有真正阅读它所说的内容。

例如,您需要操作索引对象

index = repo.index
for ( path, stage ), entry in index.entries.iteritems: pass
index.add(['SOMEFILE'])
new_commit = index.commit("YOUR COMMIT MESSAGE")
#do somethign with new commit    

我发现的另一个例子

import git
repo = git.Repo( '/home/me/repodir' )
print repo.git.status()
# checkout and track a remote branch
print repo.git.checkout( 'origin/somebranch', b='somebranch' )
# add a file
print repo.git.add( 'somefile' )
# commit
print repo.git.commit( m='my commit message' )
# now we are one commit ahead
print repo.git.status()
# now push
© www.soinside.com 2019 - 2024. All rights reserved.