Pushing local branch to remote branch

2020-07-10 20:01发布

问题:

I created new repository in my Github repository.

Using the gitpython library I'm able to get this repository. Then I create new branch, add new file, commit and try to push to the new branch.

Please check be code below:

import git
import random
import os

repo_name = 'test'
branch_name = 'feature4'

remote_repo_addr_git = 'git@repo:DevOps/z_sandbox1.git'

no = random.randint(0,1000)
repo = git.Repo.clone_from(remote_repo_addr_git, repo_name)
new_branch = repo.create_head(branch_name)
repo.head.set_reference(new_branch)
os.chdir(repo_name)
open("parasol" + str(no), "w+").write(str(no)) # this is added
print repo.active_branch
repo.git.add(A=True)
repo.git.commit(m='okej')
repo.git.push(u='origin feature4')

Everything working fine until last push method. I got this error:

stderr: 'fatal: 'origin feature4' does not appear to be a git repository fatal: Could not read from remote repository.

Please make sure you have the correct access rights and the repository exists.'

I'm able to run this method from command line and it's working fine:

git puth -u origin feature4

But it doesn't work in Python.

回答1:

This worked for me:

repo.git.push("origin", "feature4")