Skip to content

Test whether a directory is a git work tree

Return 0 if the current or given directory is inside a git work tree.

18th August 2026

is_git_repo() {
  local retval

  (
    if [[ -n $1 ]]; then
      if [[ ! -d $1 ]]; then
        return 1
      fi
      cd "${1}" || return 1
    fi

    if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
      retval=0
    else
      retval=1
    fi
    return $retval
  )
}