Jimmix
Jimmix

Reputation: 6506

git check if working copy repository exists and validate it

  1. 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.

  2. 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?

Upvotes: 3

Views: 2526

Answers (1)

torek
torek

Reputation: 488193

For item 1, use git rev-parse --is-inside-work-tree. This:

  • prints false if you are in a Git repository but not within its work-tree (i.e., are in a bare repository);
  • prints true if you are in a Git repository that has a work-tree; or
  • errors (prints fatal: 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".

Upvotes: 3

Related Questions