Check if current directory is a Git repository


I am writing a series of scripts for Git management in zsh.

How do I check if the current directory is a Git repository? (When I'm not in a Git repo, I don't want to execute a bunch of commands and get a bunch of fatal: Not a git repository responses).


You can use:

git rev-parse --is-inside-work-tree

Which will print 'true' to STDOUT if you are in a git repos working tree.

Note that it still returns output to STDERR if you are outside of a git repo (and does not print 'false').

Taken from this answer:

Why does git revert complain about a missing -m option?

Visualizing branch topology in Git