通过 GitPython 推送 Git

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41836988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:34:49  来源:igfitidea点击:

Git push via GitPython

pythongitpushgitpython

提问by amigo

I have this code in Python (using "import git"):

我在 Python 中有这个代码(使用“import git”):

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

Now I want to push this commit. I've tried a lot with no success. The Python command should be similar to this Bash command:

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

git push origin HEAD:refs/for/master

回答by BlackBeard

Following is the code to git add, git commitand then git pushusing GitPython.

以下是git addgit commit然后git push使用 GitPython的代码。

Install GitPythonusing pip install gitpython.

安装GitPython使用pip install 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()

回答by Shahaji

You can try the following. It may have your problem solved...

您可以尝试以下操作。它可能已经解决了您的问题...

repo.git.pull('origin', new_branch)
repo.git.push('origin', new_branch)

回答by Marc

This can be achieved by using Index (documented a little bit here) like so:

这可以通过使用索引(这里有一点记录)来实现,如下所示:


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()

回答by imolit

I had the same problem. I solved it by calling

我有同样的问题。我通过调用解决了它

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

回答by Kabard

Looking at the documentation page of gitpythonhttp://gitpython.readthedocs.io/en/stable/tutorial.html. You have to define a remote repo with something like origin = repo.create_remote('origin', repo.remotes.origin.url)

查看gitpythonhttp://gitpython.readthedocs.io/en/stable/tutorial.html的文档页面。你必须用类似的东西定义一个远程仓库origin = repo.create_remote('origin', repo.remotes.origin.url)

then origin.pull()

然后 origin.pull()

I would look at the whole example in the documentation in the section "Handling Remotes"

我会在“处理遥控器”部分的文档中查看整个示例

Here is the full example from the documentation

这是文档中的完整示例

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