使用GitPython进行Git push

22

我在Python中使用“import git”编写了以下代码:

repo = git.Repo("my_repository")
repo.git.add("bla.txt")
repo.git.commit("my commit description")

现在我想提交这个修改。我尝试了很多次都没有成功。Python 命令应该类似于这个 Bash 命令:

git push origin HEAD:refs/for/master
5个回答

33

以下是使用GitPython进行 git addgit commitgit push 的代码。

使用pip install gitpython安装GitPython

from git import Repo

PATH_OF_GIT_REPO = r'path\to\your\project\folder\.git'  # make sure .git folder is properly configured
COMMIT_MESSAGE = 'comment from python script'

def git_push():
    try:
        repo = Repo(PATH_OF_GIT_REPO)
        repo.git.add(update=True)
        repo.index.commit(COMMIT_MESSAGE)
        origin = repo.remote(name='origin')
        origin.push()
    except:
        print('Some error occured while pushing the code')    

git_push()

有没有一种通过Python输入密码的方法? - nerdguy
3
@nerdguy 提示:在 Git SSH 上做一些小搜索,会让生活变得更加轻松和安全。无需使用密码和 HTTP :) - aimme
1
使用Git SSH环境设置配置Git,使其能够在命令行中工作而无需用户输入用户名/密码。将密码保持在代码之外。 - CodingMatters
我收到的推送是空的吗? - James
Git pull的API(https://gitpython.readthedocs.io/en/stable/reference.html#git.remote.Remote.pull)中提到“kwargs-要传递给git-pull的其他参数”。那么我是否可以直接使用`repo.git.pull()`而不使用[`remote()`](https://gitpython.readthedocs.io/en/stable/reference.html#git.repo.base.Repo.remote)来指定“远程”分支?我看到Shaghaji的回答,但对于位置参数我并不清楚。我在文档中没有看到它。 - Timo

5
您可以尝试以下方法。它可能会解决您的问题...
repo.git.pull('origin', new_branch)
repo.git.push('origin', new_branch)

4
这可以通过使用索引(在此有一些文档)来实现,如下所示:

from git import Repo
repo = Repo('path/to/git/repo')  # if repo is CWD just do '.'

repo.index.add(['bla.txt'])
repo.index.commit('my commit description')
origin = repo.remote('origin')
origin.push()

1

我曾经遇到过同样的问题。我通过调用解决了它。

repo.git.push("origin", "HEAD:refs/for/master")

1

查看 gitpython 的文档页面 http://gitpython.readthedocs.io/en/stable/tutorial.html。您需要使用类似于 origin = repo.create_remote('origin', repo.remotes.origin.url) 的方式定义一个远程仓库。

然后执行 origin.pull()

我建议查看文档中“处理远程仓库”部分的完整示例。

以下是来自文档的完整示例:

empty_repo = git.Repo.init(osp.join(rw_dir, 'empty'))
origin = empty_repo.create_remote('origin', repo.remotes.origin.url)
assert origin.exists()
assert origin == empty_repo.remotes.origin == empty_repo.remotes['origin']
origin.fetch()                  # assure we actually have data. fetch() returns useful information
# Setup a local tracking branch of a remote branch
empty_repo.create_head('master', origin.refs.master)  # create local branch "master" from remote "master"
empty_repo.heads.master.set_tracking_branch(origin.refs.master)  # set local "master" to track remote "master
empty_repo.heads.master.checkout()  # checkout local "master" to working tree
# Three above commands in one:
empty_repo.create_head('master', origin.refs.master).set_tracking_branch(origin.refs.master).checkout()
# rename remotes
origin.rename('new_origin')
# push and pull behaves similarly to `git push|pull`
origin.pull()
origin.push()
# assert not empty_repo.delete_remote(origin).exists()     # create and delete remotes

2
谢谢,我看过这个例子,但它对我还是不起作用... 第一行有一个错误 origin = repo.create_remote('origin', repo.remotes.origin.url):"git remote add origin ssh:........ 返回了 128 的退出状态:fatal: remote origin already exists。有什么建议吗? - amigo
如果有人能帮忙,我会非常感激 :) - amigo
这个例子毫无意义。repo.create_remote('origin', repo.remotes.origin.url)是通过引用“origin”远程的url来创建“origin”远程???如果repo.remotes.origin不为None,则您已经拥有了原始远程,只需简单地执行repo.remotes.origin.push()即可。 - StFS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接