I'm looking for a way to test local directory against presence of a git repository.
I would not like just to check by sh/bash if the .git
sub directory exist but rather use dedicated git command if there is a one.
I want to validate potentially found git repository in that local directory by a git command.
Do you know any git commands to do that?
For item 1, use git rev-parse --is-inside-work-tree
. This:
false
if you are in a Git repository but not within its work-tree (i.e., are in a bare repository);true
if you are in a Git repository that has a work-tree; orfatal: not a git repository (or any parent up to...
to stderr) if you are not in a Git repository.Note, however, that if you are in a Git repository whose work-tree is damaged or modified in some way, Git will print true
here. If you pass --git-dir=
and a path to a valid repository, Git always thinks you are in a work-tree (because you are). The exit status of this command is zero if it printed either true
or false
, and nonzero if it produced an error.
For item 2—"validating" the repository—this depends entirely on what you mean by "validate".