Skip to content

Instantly share code, notes, and snippets.

@fralau fralau/
Last active Apr 11, 2018

What would you like to do?
Python: know whether the current directory is part of a git repo

How to know wether current directory is part of a git repo


The git rev-parse command returns 0 if the current directory is in a git repo and an error code otherwise. It is thus necessary to call the bash command, suppressing stdout and stderr, in order to get the return code.


import os, subprocess
def is_git_repo():
    "Check whether this is a git repo"
    # we use the exit code
    with open(os.devnull, "w") as d:
        return not bool('git rev-parse', shell=True,
                        stdout=d, stderr=d))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.