gitpython - how to check if a remote branch exists?

萝らか妹 提交于 2020-01-24 15:59:26

问题


I'm new to gitpython and haven't been able to find a reference to this anywhere. What I'm looking to do is something like:

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

Any suggestions?


回答1:


This may not work, but give it a shot let me know how it goes:

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

print(does_exist)

This may also work but give it a try:

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



回答2:


Thanks Muadh! I was able to get this to work:

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


来源:https://stackoverflow.com/questions/50707781/gitpython-how-to-check-if-a-remote-branch-exists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!