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.
So how can you tell if a directory is within a git repository? Exit code of 0 means it's a git repository. Any other code (e.g., 128 ) means it's not.
git directory is a configuration file for git. Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files.
The Local Repository is the . git/ subdirectory inside the Working Directory.
To get Git to recognize an empty directory, the unwritten rule is to put a file named . gitkeep in it. Git will see the . gitkeep file in the otherwise empty folder and make that folder part of the next commit or push.
Use git -C <path> rev-parse
. It will return 0 if the directory at <path>
is a git repository and an error code otherwise.
Further Reading:
rev-parse
-C <path>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With