1

How can I run the below command in Python 3.8 and get the output?

Command:

git remote show [URL for remote Github repo] | sed -n '/HEAD branch/s/.*: //p'

Command purpose:

To get the default branch from remote Github repo also trying to run this outside git directory

Below are the commands I have tried till now, but it gives error: "fatal: not a git repository (or any of the parent directories): .git":

import os
p = os.system("git remote show https://github.com/docker/compose | grep 'HEAD branch' | cut -d' ' -f5")
import subprocess

subprocess.run(["git", "remote", "show", "https://github.com/docker/compose", "|", "grep", "'HEAD branch'", "|", "cut", "-d' '", "-f5" ])

I need the command to execute even outside the Git directory, please help me

1 Answer 1

2

To run a command you can use subprocess.run which can return all the information you want.

But if you just want information from git use a ready-made module like GitPython instead

Sign up to request clarification or add additional context in comments.

4 Comments

subprocess.run did not work, if you know the exact command to execute please share
What is the error ?
error: fatal: not a git repository (or any of the parent directories): .git I fixed it using git init but it will create .git directory which i don't want is there any way to execute this without creating a .git directory?
Did you try to execute the command in the terminal? At the same level / folder where you run the Python program? And no, most Git commands require a local repository, including the one in the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.