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?
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?
GitPython has references to all the branches. A user can iterate to check if the branch is part of that reference or not.
To print all the branches use this as reference:
for ref in repo.references:
print("Branch Name is: ", ref.name)
Similarly for your purpose, if you can do something like
for ref in repo.references:
if branchName == ref.name:
Do likely something
else:
Do unlikely something
To those like me that want to call using a remote url without a local repo
import git
def _ls_remote_branches(url):
remote_branches = []
g = git.cmd.Git()
for ref in g.ls_remote(url).split("\n"):
if "head" in ref:
hash_ref_list = ref.split("\t")
remote_branches.append(
hash_ref_list[1].split("/")[-1]
)
return remote_branches
Edit 1: other answers did not work for me because I was looking for something that does not require a local git repo.
Edit 2: This was based on the upvoted answer to another question here