我使用 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) 给定repo来自 GitPython 的 a,我如何创建一个新的本地分支,添加一些文件,并使用 GitPython 将其推送到远程?
创建一个repo:
from git import *
curr_dir = os.path.dirname(os.path.realpath(__file__))
repo = Repo(curr_dir)
Run Code Online (Sandbox Code Playgroud)
现在,我只是使用subprocess:
def publish_changes_to_git(commit_msg):
curr_time = time.time()
ts = datetime.datetime.fromtimestamp(curr_time).strftime('%Y-%m-%d-%H-%M-%S')
branch_name = "auto-commit-{ts}".format(ts=ts)
subprocess.check_output(["git", "checkout", "-b", branch_name])
subprocess.check_output(["git", "add", SOME_PATH])
subprocess.check_output(
["git", "commit", "-m", "auto-git-commit: {msg}".format(msg=commit_msg)])
Run Code Online (Sandbox Code Playgroud) 我在 Github 存储库中创建了新存储库。
使用 gitpython 库,我可以获得这个存储库。然后我创建新分支,添加新文件,提交并尝试推送到新分支。
请检查以下代码:
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')
Run Code Online (Sandbox Code Playgroud)
一切正常,直到最后一个推送方法。我收到此错误:
stderr: 'fatal: 'origin feature4' 似乎不是 git 存储库 致命:无法从远程存储库读取。
请确保您拥有正确的访问权限并且存储库存在。
我可以从命令行运行这个方法并且它工作正常:
git puth -u origin feature4
Run Code Online (Sandbox Code Playgroud)
但它在 Python 中不起作用。