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

时间:2018-06-05 19:41:41

标签: python gitpython

我是gitpython的新手,并且无法在任何地方找到对此的参考。我希望做的是:

If remote branch name exists:
  do something
else:
  do something else

有什么建议吗?

4 个答案:

答案 0 :(得分:1)

这可能不起作用,但请试一试让我知道它是怎么回事:

does_exist = True
try:
    repo.git.checkout('branch_name')
except repo.exc.GitCommandError:
    does_exist = False

print(does_exist)

这可能也有用,但试一试:

repo.git.rev_parse('--verify', 'branch_name')

答案 1 :(得分:0)

谢谢Muadh!我能够让这个工作:

try:
    repo.git.checkout( 'origin/' + branch_name, b=branch_name )
except:
    repo.git.checkout( 'origin/master', b=branch_name )

答案 2 :(得分:0)

GitPython引用了所有分支。用户可以重复检查分支是否属于该引用的一部分。

要打印所有分支,请以此作为参考:

for ref in repo.references:
    print("Branch Name is: ", ref.name)

类似地,如果您可以做类似的事情

for ref in repo.references:
    if branchName == ref.name:
        Do likely something
    else:
        Do unlikely something

答案 3 :(得分:0)

最简单的似乎是:

allBranches = git.Git().branch("-all").split()
"<testedBranch>" in allBranches
>>> true/false
  • 这将分支嵌入到序列协议的所有标准技术中
相关问题