我正在为 zsh 中的 Git 管理编写一系列脚本。
如何检查当前目录是否是 Git 存储库?(当我不在 Git 存储库中时,我不想执行一堆命令并得到一堆fatal: Not a git repository
响应)。
从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;
您可以将其包装在一个函数中,也可以在脚本中使用它。
压缩成适用于bash和zsh的单行条件
[ -d .git ] && echo .git || git rev-parse --git-dir > /dev/null 2>&1
您可以使用:
git rev-parse --is-inside-work-tree
如果您在 git repos 工作树中,它将打印“true”。
请注意,如果您在 git repo 之外(并且不打印“false”),它仍会将输出返回到 STDERR。
使用 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)"
可以确定当前目录是否是顶级目录。
或者你可以这样做:
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
[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]
不包含任何冗余操作并在-e
模式下工作。
git rev-parse
成功,而忽略其输出。
git
命令都只在工作树内有效。因此,出于编写脚本的目的,您很可能不仅对“git repo”内部感兴趣,而且对工作树内部感兴趣。不确定是否有公开访问/记录的方式来执行此操作(有一些内部 git 函数可以在 git 源本身中使用/滥用)
你可以做类似的事情;
if ! git ls-files >& /dev/null; then
echo "not in git"
fi
另一种解决方案是检查命令的退出代码。
git rev-parse 2> /dev/null; [ $? == 0 ] && echo 1
如果您在 git 存储库文件夹中,这将打印 1。
这个答案提供了一个示例 POSIX shell 函数和一个用法示例来补充@jabbie 的答案。
is_inside_git_repo() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1
}
git
如果它在 git 存储库中,则返回errorlevel 0
,否则返回 errorlevel 128
。true
(false
如果它在 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
为什么不使用退出代码?如果当前目录中存在 git 存储库,则git branch
和git tag
命令返回退出代码 0;否则,将返回非零退出代码。这样,您可以确定 git 存储库是否存在。简单地说,您可以运行:
git tag > /dev/null 2>&1
优点:便携。它适用于裸存储库和非裸存储库,以及 sh、zsh 和 bash。
git tag
:获取存储库的标签以确定是否存在。> /dev/null 2>&1
:防止打印任何内容,包括正常和错误输出。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
这对我有用。您仍然会收到错误,但它们很容易被压制。它也适用于子文件夹!
git status >/dev/null 2>&1 && echo Hello World!
如果您需要有条件地做更多事情,您可以将其放在 if then 语句中。
# 检查是否是 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
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 中。查看帮助类型
##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)
在 linux 的终端或 Windows 的 cmd 中打开该目录,然后输入git status
2021 年应该足以告诉您您在 git repo 中并且您有x
多个分支和x
提交数。