Skip to content

Instantly share code, notes, and snippets.

@rreece
Last active April 20, 2021 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rreece/efc569ee93cd6e716afedc07b627928c to your computer and use it in GitHub Desktop.
Save rreece/efc569ee93cd6e716afedc07b627928c to your computer and use it in GitHub Desktop.
Check if a file is tracked by git
def path_is_in_git(repo, path):
"""
Check if path is tracked by git.
"""
returncode = None
try:
cmd = 'git ls-files --error-unmatch %s' % (path)
_ = subprocess.check_output(cmd,
cwd=repo,
shell=True,
stderr=subprocess.DEVNULL)
returncode = 0
except subprocess.CalledProcessError as e:
returncode = e.returncode
return returncode == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment