GitPython:git push - 设置上游

6
我使用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()

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

To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/pythontest
2个回答

2

如果要推送新分支,您需要运行git push --set-upstream origin branch_name,您可以在git文档https://git-scm.com/docs/git-push中了解--set-upstream的相关信息。这应该可以为gitpython完成工作:

def git_clone():
    branch_name = "feature/pythontest"
    repo = Repo.clone_from(<git-repo>, <local-repo>)
    repo.git.checkout("-b", branch_name)
    repo.git.add(repo.working_dir)
    commit_output = repo.git.commit(m="Commit msg")
    push_output = repo.git.push('--set-upstream', repo.remote().name, branch_name)

希望这能帮到你!

1

origin.push() 不知道如何将本地分支与远程仓库中的分支匹配,因此您需要通过 refspec 指定。

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

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

您可以在fetch定义的这里找到更多详细信息。


1
如果你有一个本地的主分支,并且想要跟踪名为“origin”的远程仓库上的主分支,那么通常情况下你会想要使用refspec="master:master"。(当我使用master:origin时,我的'origin'远程仓库也有了一个名为'origin'的分支,而这个分支被我的本地分支'master'所跟踪) - Tim

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