gpt4 book ai didi

python - 使用 Gitpython 将新的本地分支推送到远程

转载 作者:行者123 更新时间:2023-12-03 16:40:07 33 4
gpt4 key购买 nike

我查看了一些引用资料,但仍然遇到问题:

我想克隆一个远程仓库,创建一个新分支,然后使用 GitPython 将新分支推送回远程。

这似乎有效:

import git
import subprocess

nm_brnch = 'new_branch'

# Clone
repo_url = r'my_remote.git'
repo = git.Repo.clone_from(repo_url, dnm_wrk, branch=r'some_branch')

# Create new branch
git = repo.git
git.checkout('HEAD', b=nm_brnch)

# Push new branch to remote
subprocess.call(f'git push -u origin {nm_brnch}')

但它很丑,因为它使用 subprocess ,而不是使用 GitPython。

我尝试使用 GitPython,但没有成功:
repo.head.set_reference(nm_brnch)
repo.git.push("origin", nm_brnch)

我查阅了以下引用资料:
  • Pushing local branch to remote branch
  • Use GitPython to Checkout a new branch and push to remote
  • Related GitHub issue/question
  • Tutorial from official docs
  • 最佳答案

    我正在使用 gitpython==2.1.11使用 Python 3.7。下面是我的推送功能,我首先尝试高级推送,然后根据需要尝试低级推送。请注意我如何检查任一命令的返回值。我还记录推送操作,这解释了每一步发生的情况。

    class GitCommandError(Exception):
    pass

    class Git:
    def _commit_and_push_repo(self) -> None:
    repo = self._repo
    remote = repo.remote()
    remote_name = remote.name
    branch_name = repo.active_branch.name

    # Note: repo.index.entries was observed to also include unpushed files in addition to uncommitted files.
    log.debug('Committing repository index in active branch "%s".', branch_name)
    self._repo.index.commit('')
    log.info('Committed repository index in active branch "%s".', branch_name)

    def _is_pushed(push_info: git.remote.PushInfo) -> bool:
    valid_flags = {push_info.FAST_FORWARD, push_info.NEW_HEAD} # UP_TO_DATE flag is intentionally skipped.
    return push_info.flags in valid_flags # This check can require the use of & instead.

    push_desc = f'active branch "{branch_name}" to repository remote "{remote_name}"'
    log.debug('Pushing %s.', push_desc)
    try:
    push_info = remote.push()[0]
    except git.exc.GitCommandError: # Could be due to no upstream branch.
    log.warning('Failed to push %s. This could be due to no matching upstream branch.', push_desc)
    log.info('Reattempting to push %s using a lower-level command which also sets upstream branch.', push_desc)
    push_output = repo.git.push('--set-upstream', remote_name, branch_name)
    log.info('Push output was: %s', push_output)
    expected_msg = f"Branch '{branch_name}' set up to track remote branch '{branch_name}' from '{remote_name}'."
    if push_output != expected_msg:
    raise RepoPushError(f'Failed to push {push_desc}.')
    else:
    is_pushed = _is_pushed(push_info)
    logger = log.debug if is_pushed else log.warning
    logger('Push flags were %s and message was "%s".', push_info.flags, push_info.summary.strip())
    if not is_pushed:
    log.warning('Failed first attempt at pushing %s. A pull will be performed.', push_desc)
    self._pull_repo()
    log.info('Reattempting to push %s.', push_desc)
    push_info = remote.push()[0]
    is_pushed = _is_pushed(push_info)
    logger = log.debug if is_pushed else log.error
    logger('Push flags were %s and message was "%s".', push_info.flags, push_info.summary.strip())
    if not is_pushed:
    raise RepoPushError(f'Failed to push {push_desc} despite a pull.')
    log.info('Pushed %s.', push_desc)

    关于python - 使用 Gitpython 将新的本地分支推送到远程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53447788/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com