Mik*_*ike 10 python git gitpython
我没有看到在此模块中签出或列出远程/本地分支的选项:https://gitpython.readthedocs.io/en/stable/
小智 11
对于那些只想打印远程分支的人:
# Execute from the repository root directory
repo = git.Repo('.')
remote_refs = repo.remote().refs
for refs in remote_refs:
print(refs.name)
Run Code Online (Sandbox Code Playgroud)
你做完之后
from git import Git
g = Git()
Run Code Online (Sandbox Code Playgroud)
(可能还有一些其他命令用于初始化g到您关心的存储库)所有属性请求g都或多或少地转换为调用git attr *args.
因此:
g.checkout("mybranch")
Run Code Online (Sandbox Code Playgroud)
应该做你想做的事.
g.branch()
Run Code Online (Sandbox Code Playgroud)
将列出分支机构.但请注意,这些是非常低级别的命令,它们将返回git可执行文件将返回的确切代码.因此,不要指望一个好的清单.我只是一串几行,一行有一个星号作为第一个字符.
可能有更好的方法在库中执行此操作.在repo.py例如是一个特殊的active_branch命令.你需要稍微浏览一下源代码并自己寻找.
要列出目前您可以使用的分支:
from git import Repo
r = Repo(your_repo_path)
repo_heads = r.heads # or it's alias: r.branches
Run Code Online (Sandbox Code Playgroud)
r.heads返回git.util.IterableList(继承list)git.Head对象,因此您可以:
repo_heads_names = [h.name for h in repo_heads]
Run Code Online (Sandbox Code Playgroud)
并结账,例如.master:
repo_heads['master'].checkout()
# you can get elements of IterableList through it_list['branch_name']
# or it_list.branch_name
Run Code Online (Sandbox Code Playgroud)
在是问题中提到模块GitPython,其移动从gitorious到Github上.
我有类似的问题。就我而言,我只想列出本地跟踪的远程分支。这对我有用:
import git
repo = git.Repo(repo_path)
branches = []
for r in repo.branches:
branches.append(r)
# check if a tracking branch exists
tb = t.tracking_branch()
if tb:
branches.append(tb)
Run Code Online (Sandbox Code Playgroud)
如果需要所有远程分支,我更喜欢直接运行 git:
def get_all_branches(path):
cmd = ['git', '-C', path, 'branch', '-a']
out = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
return out
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16568 次 |
| 最近记录: |