Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitPython : git push - set upstream

Im using GitPython to clone a master branch and do a checkout of a feature branch, I do my local updates, commit and push back to git. The code snippet looks like below,

Note : my branch name is 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()

When I execute the script, I get the below error,

To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/pythontest
like image 969
Rahul gone mad Avatar asked Aug 06 '26 03:08

Rahul gone mad


1 Answers

origin.push() doesn't know how to match the local branch to the one in the origin, so you need to specify it through refspec:

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

master is your local branch and origin the target.

You can find more details here in the fetch definition.

like image 182
Nick Gkloumpos Avatar answered Aug 07 '26 17:08

Nick Gkloumpos