Ataraxia Ataraxia - 2 months ago 37
Git Question

Check if directory is git repository (without having to cd into it)

What's a shell command I can use to, using the full directory path, determine whether or not a given directory is a git repository? Specifically, I'd like to be able to do this without being in the directory, and without having to cd into it. I'd also like to be able to do it with a command that returns a simple "true" or "false" (much the way that rev-parse --is-inside-work-tree does), but it's not a requirement.

Answer

Use git -C <path> rev-parse. It will return 0 if the directory at <path> is a git repository and an error code otherwise.