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 thi
Any directory in the system could be a git working copy. You can use an directory as if it contained a .git
subdirectory by setting the GIT_DIR
and GIT_WORK_TREE
environment variables to point at an actual .git
directory and the root of your working copy, or use the --git-dir
and --work-tree
options instead. See the git man page for more details.
Adding to @Walk's comments,
git status
will throw fatal message if its not a repo.
echo $?
will help to capture of its an ERROR or not.
If its a git repo then echo $?
will be 0 else 128 will be the value
We can also try git status
command and if the output is like :
fatal: Not a git repository (or any of the parent directories): .git
Then, the directory is not a git repository.
Use git -C <path> rev-parse
. It will return 0 if the directory at <path>
is a git repository and an error code otherwise.