gitpython:确定与(跟踪)分支关联的远程

gitpython: determine the remote associated to a (tracking) branch

我想确定与当前(跟踪分支)关联的远程分支

我找到的解决方案有效,但感觉很奇怪,我必须解析配置才能实现我想要的。

有没有更优雅的方案?

repo = git.Repo(path) 
branch = repo.active_branch
cfg =  branch.config_reader().config  
# hand crafting the section name in next line just seems clumsy
remote= cfg.get(f'branch "{branch.name}"', "remote") 

也许git.Head.tracking_branch() and git.Reference.remote_name可以给你想要的东西?

例如

repo = git.Repo(path) 
branch = repo.active_branch
remote_name = branch.tracking_branch().remote_name