python - 使用 GitPython 模块获取远程 HEAD 分支

标签 python git gitpython

我正在尝试使用 GitPython 编写一些 Python 脚本,我可以用它来简化我管理许多分支的日常任务。

在编写复杂脚本方面,我对 Python 还是个新手。

这是我使用的 API:GitPython API doc

我想在 GitPython 中编写它,只需执行以下操作并解析出显示 HEAD 远程分支指向的部分。换句话说,我想基本上得到 remotes/origin/HEAD

$ git branch -a
  master
* branch_to_remove
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/testing

我多次浏览 API 文档,起初我很难理解这些 API 文档的 Python 格式,除了 remote_head 类 git.refs.reference.Reference(repo, path, check_path=True)

但我什至不知道如何调用/初始化它。

这是我到目前为止所做的,你可以告诉我我正在尝试做什么,简单地重置为“无分支”状态并删除我所在的当前分支:

import git
from git import *
repo = git.Repo("/some/path/testing")
repo.git.branch()
[some code to get the remotes/origin/HEAD, set it to remoteHeadBranch ]
repo.git.checkout(remoteHeadBranch)  # this should reset the Git back to 'no branch' state
repo.git.checkout(D="branch_to_remove")

非常感谢任何帮助!

谢谢。

最佳答案

我刚看到你的问题我想知道这个 gitPython,看起来是一个非常好的工具,我在 GitPython 文档中寻找这个特定的问题,但运气不好,但如果你在 github 上搜索,你会在那里看到很多测试并且有一个测试。

当你搜索“remove new branch”时你会看到这样的东西:

# remove new branch
Head.delete(new_remote_branch.repo, new_remote_branch)

GitPython Reference

关于python - 使用 GitPython 模块获取远程 HEAD 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10991639/

相关文章:

Git:从命名远程获取和从 URL 获取有什么区别?

node.js - 作为生成 Node 进程启动时显示来自 GIt 的反馈

python - 阻止 GitPython 在尝试克隆不存在的远程存储库时询问凭据

python - gitpython - 如何检查远程分支是否存在?

python - Pandas/Numpy - 如何获取每列的计数并将其与每列进行比较并写入 csv?

python - 在 Ubuntu 中安装 lxml 时出现问题

Python从url下载大型xml文件以获取前10个条目

git - 推送到 friend 的仓库

python - 当有东西要获取/拉取时与没有东西时从 gitPython 返回的 FetchInfo 对象有什么区别?

用于桌面数据库应用程序的 Python 框架