Am I in a Git repository?
Checking from a script whether you're inside a Git repository or not is slightly more involved than you might hope.
The complexity arises from the fact that:
- in a standard Git repository, you can either be somewhere in the working
tree, or in the
.gitdirectory itself, and
- you might also be in a bare Git repository wth no working tree, or a
worktree associated with a bare repository (which doesn't have a
Ideally I'd like a command that exits silently with success if inside any of the above locations, and fails silently otherwise.
The top answer to this StackOverflow question on how to check if the current directory is a Git repository is to use:
and look at the output, since it will print
true to standard out if you're
inside a Git working tree, and fail with a message to standard error if run
outside a Git repository.
The main problem with this is that it won't print
true when inside
a bare Git repository, nor when inside the
.git folder itself within a
normal non-bare repository. However it will still succeed in those cases,
so one way to get what I want would simply be to send the output and error to
/dev/null and just check the exit status. However that's then true of
any Git command (or a large number of them anyway; some for example fail
when inside a bare repository, like
git status). So I might as well just run
with no options or arguments. As far as the exit status is concerned, this
will behave the same as
git rev-parse --is-inside-work-tree. (So would
git worktree list,
git tag, or
git remote show. At least
rev-parse is a plumbing not a porcelain command, and in its simplest form
doesn't give any output.)
Another possibility (the accepted answer to the above question) is to run
and test the exit status.
In fact this comes from the Bash completion file included in Git itself.
(Actually, the slightly more complicated answer checks for the existence of the
.git directory first, and if that doesn't exist, tries
git rev-parse --git-dir; the reason for this is presumably speed, in the
probably large subset of cases where
.git exists inside the current
directory, you can avoid running the Git command; for Bash completion this
seems like a worthwhile optimisation, but I can ignore it here.)
But again, since we're ignoring any output from the command and just testing for
success/failure, there's no advantage to adding the
--git-dir option, only
to yield output that you then have to explicitly throw away.
There are other similar flags to
rev-parse for this kind of introspection;
false, as well as:
which print the location of the Git repository. But all seem to me to be
predicated on being somewhere inside a Git repository (in the general sense),
with the purpose of providing more detailed information, and error out
otherwise. So the simple reliable way of checking whether you are somewhere
inside a Git repository, when that's all you care about, seems to boil down to
running any Git command which should succeed if so, and checking the exit
status; for which bare
rev-parse seems a clean and simple choice.