268

我正在为 zsh 中的 Git 管理编写一系列脚本。

如何检查当前目录是否是 Git 存储库?(当我不在 Git 存储库中时,我不想执行一堆命令并得到一堆fatal: Not a git repository响应)。

4

14 回答 14

192

从bash补全文件中复制过来,下面是一个幼稚的做法

# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.

if [ -d .git ]; then
  echo .git;
else
  git rev-parse --git-dir 2> /dev/null;
fi;

您可以将其包装在一个函数中,也可以在脚本中使用它。

压缩成适用于bashzsh的单行条件

[ -d .git ] && echo .git || git rev-parse --git-dir > /dev/null 2>&1
于 2010-02-01T21:57:38.103 回答
189

您可以使用:

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

如果您在 git repos 工作树中,它将打印“true”。

请注意,如果您在 git repo 之外(并且不打印“false”),它仍会将输出返回到 STDERR。

取自这个答案:https ://stackoverflow.com/a/2044714/12983

于 2013-06-04T18:35:32.303 回答
60

使用 git rev-parse --git-dir

if git rev-parse --git-dir > /dev/null 2>&1; then
  : # This is a valid git repository (but the current working
    # directory may not be the top level.
    # Check the output of the git rev-parse command if you care)
else
  : # this is not a git repository
fi

编辑: git-rev-parse现在(从 1.7.0 开始)支持--show-toplevel,因此您if test "$(pwd)" = "$(git rev-parse --show-toplevel)"可以确定当前目录是否是顶级目录。

于 2010-02-02T15:48:04.480 回答
33

或者你可以这样做:

inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"

if [ "$inside_git_repo" ]; then
  echo "inside git repo"
else
  echo "not in git repo"
fi
于 2016-06-29T01:25:17.263 回答
13

基于@Alex Cory 的回答

[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]

不包含任何冗余操作并在-e模式下工作。

  • 正如@go2null 所指出的,这在裸仓库中不起作用。如果您出于某种原因想要使用裸仓库,您可以只检查是否git rev-parse成功,而忽略其输出。
    • 我不认为这是一个缺点,因为上面的行是为脚本编写的,几乎所有git命令都只在工作树内有效。因此,出于编写脚本的目的,您很可能不仅对“git repo”内部感兴趣,而且对工作树内部感兴趣。
于 2018-11-19T02:32:40.530 回答
8

不确定是否有公开访问/记录的方式来执行此操作(有一些内部 git 函数可以在 git 源本身中使用/滥用)

你可以做类似的事情;

if ! git ls-files >& /dev/null; then
  echo "not in git"
fi
于 2010-02-01T21:59:06.947 回答
7

另一种解决方案是检查命令的退出代码。

git rev-parse 2> /dev/null; [ $? == 0 ] && echo 1

如果您在 git 存储库文件夹中,这将打印 1。

于 2015-12-14T10:14:16.710 回答
6

这个答案提供了一个示例 POSIX shell 函数和一个用法示例来补充@jabbie 的答案

is_inside_git_repo() {
    git rev-parse --is-inside-work-tree >/dev/null 2>&1
}

git如果它在 git 存储库中,则返回errorlevel 0,否则返回 errorlevel 128truefalse如果它在 git 存储库中,它也会返回。)

使用示例

for repo in *; do
    # skip files
    [ -d "$repo" ] || continue
    # run commands in subshell so each loop starts in the current dir
    (
        cd "$repo"
        # skip plain directories
        is_inside_git_repo || continue
        printf '== %s ==\n' "$repo"
        git remote update --prune 'origin' # example command
        # other commands here
    )
done
于 2018-10-22T22:14:23.337 回答
5

为什么不使用退出代码?如果当前目录中存在 git 存储库,则git branchgit tag命令返回退出代码 0;否则,将返回非零退出代码。这样,您可以确定 git 存储库是否存在。简单地说,您可以运行:

git tag > /dev/null 2>&1

优点:便携。它适用于裸存储库和非裸存储库,以及 sh、zsh 和 bash。

解释

  1. git tag:获取存储库的标签以确定是否存在。
  2. > /dev/null 2>&1:防止打印任何内容,包括正常和错误输出。

TLDR ( R e a l l y ? ! ):check-git-repo

例如,您可以创建一个check-git-repo具有以下内容的文件,使其可执行并运行它:

#!/bin/sh

if git tag > /dev/null 2>&1; then
    echo "Repository exists!";
else
    echo "No repository here.";
fi
于 2019-08-03T16:34:37.853 回答
4

这对我有用。您仍然会收到错误,但它们很容易被压制。它也适用于子文件夹!

git status >/dev/null 2>&1 && echo Hello World!

如果您需要有条件地做更多事情,您可以将其放在 if then 语句中。

于 2014-01-19T04:31:54.523 回答
4

# 检查是否是 git repo

if [ $(git rev-parse --is-inside-work-tree) = true ]; then
    echo "yes, is a git repo"
    git pull
else
    echo "no, is not a git repo"
    git clone url --depth 1
fi
于 2018-12-17T04:56:28.710 回答
0
if ! [[ $(pwd) = *.git/* || $(pwd) = *.git ]]; then 
  if type -P git >/dev/null; then
    ! git rev-parse --is-inside-work-tree >/dev/null 2>&1 || {
     printf '\n%s\n\n' "GIT repository detected." && git status
    }
  fi
fi

谢谢ivan_pozdeev,现在我测试了 .git 目录中的代码是否不会运行,因此不会打印出错误或错误的退出状态。

! [[ $(pwd) = .git/ || $(pwd) = *.git ]] ” 测试您是否不在.git 存储库中,然后它将运行 git 命令。内置类型命令用于检查您是否安装了 git 或者它是否在您的 PATH 中。查看帮助类型

于 2020-01-11T20:58:04.080 回答
0
##Current branch
echo $(git branch --show-current 2> /dev/null && echo '')
echo $(git branch --show-current 2> /dev/null)

##OR
GIT_DIR=$(git rev-parse --git-dir 2> /dev/null)
于 2022-02-21T20:24:13.597 回答
-2

在 linux 的终端或 Windows 的 cmd 中打开该目录,然后输入git status2021 年应该足以告诉您您在 git repo 中并且您有x多个分支和x提交数。

于 2021-07-19T01:29:01.210 回答