Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.4k points)

I am trying to get a list of all the branches available on my repository using Python with this code :

import subprocess

branches = ["All"]

command = "git branch -r"

branch_list = subprocess.check_output(command)

for branch in branch_list:

   print branch

   branches.append[branch]

What I want is to have something like :

print branches[0] # is "All"

print branches[1] # is "branch1"

print branches[2] # is "branch2"

etc etc

but instead of that I have

print branches[0] # is "All"

print branches[1] # is "b"

print branches[2] # is "r"

print branches[3] # is "a"

print branches[4] # is "n"

print branches[5] # is "c"

print branches[6] # is "h"

etc etc

Thank you for your time and your help.

1 Answer

0 votes
by (119k points)

You can try using the decode function for that:

stdout = subprocess.check_output('git branch -a'.split())

out = stdout.decode()

branches = [b.strip('* ') for b in out.splitlines()]

print(branches)

You can enroll in this Python Course by Intellipaat to learn Python.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers

500 comments

108k users

Browse Categories

...