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

xbe*_*eta 8 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
Run Code Online (Sandbox Code Playgroud)

我浏览API文档很多次,起初我无法理解这些API文档的Python的格式,我找不到任何有用的东西用来做这以外remote_headclass 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")
Run Code Online (Sandbox Code Playgroud)

任何帮助深表感谢!

谢谢.

moi*_*ega 4

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

当您搜索“删除新分支”时,您会看到类似这样的内容:

# remove new branch
Head.delete(new_remote_branch.repo, new_remote_branch)
Run Code Online (Sandbox Code Playgroud)

GitPython 参考