GitPython:git push - 设置上游

Rah*_*mad 7 python gitpython gitlab

我使用 GitPython 克隆主分支并签出功能分支,我进行本地更新、提交并推送回 git。代码片段如下所示,

注意:我的分支名称是 feature/pythontest

def git_clone():
    repo = Repo.clone_from(<git-repo>, <local-repo>)
    repo.git.checkout("-b", "feature/pythontest")
    # I have done with file updates 
    repo.git.add(update=True)
    repo.index.commit("commit")
    origin = repo.remote(name="origin")
    origin.push()
Run Code Online (Sandbox Code Playgroud)

当我执行脚本时,出现以下错误,

To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/pythontest
Run Code Online (Sandbox Code Playgroud)

小智 2

origin.push()不知道如何将本地分支与源中的分支相匹配,因此您需要通过 refspec 指定它:

origin.push(refspec="master:origin")

master 是您的本地分支,origin 是目标。

您可以在获取定义中找到更多详细信息。