我正在为zsh中的Git管理编写一系列脚本.
如何检查当前目录是否是Git存储库?(当我不在Git仓库中时,我不想执行一堆命令并得到一堆fatal: Not a git repository响应).
jab*_*bie 141
从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;
Run Code Online (Sandbox Code Playgroud)
您可以将其包装在函数中,也可以在脚本中使用它.
浓缩成适合bash或zsh的单行条件
[ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)
TM.*_*TM. 105
您可以使用:
git rev-parse --is-inside-work-tree
Run Code Online (Sandbox Code Playgroud)
如果你在git repos工作树中,那将打印'true'.
请注意,如果您位于git repo之外,它仍然会将输出返回给STDERR(并且不会打印'false').
取自这个答案:https://stackoverflow.com/a/2044714/12983
Wil*_*ell 41
使用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
Ale*_*ory 21
或者你可以这样做:
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
Run Code Online (Sandbox Code Playgroud)
iva*_*eev 11
[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]
Run Code Online (Sandbox Code Playgroud)
不包含任何冗余操作并在-e模式下工作。
git rev-parse成功,而忽略其输出。
git命令都只在工作树内有效。因此,出于编写脚本的目的,您很可能不仅对“git repo”感兴趣,而且对工作树感兴趣。为什么不使用退出代码?如果当前目录中存在 git 存储库,则git branch和git tag命令返回退出代码 0(即使没有标签或分支);否则,将返回非零退出代码。这样,您就可以确定 git 存储库是否存在。简单地说,您可以运行:
git tag > /dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)
优点:便携。它适用于裸存储库和非裸存储库,以及 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
Run Code Online (Sandbox Code Playgroud)
小智 7
不确定是否有可公开访问/记录的方法来执行此操作(有一些内部git函数可以在git源本身中使用/滥用)
你可以做点什么;
if ! git ls-files >& /dev/null; then
echo "not in git"
fi
Run Code Online (Sandbox Code Playgroud)
小智 6
另一种解决方案是检查命令的退出代码.
git rev-parse 2> /dev/null; [ $? == 0 ] && echo 1
Run Code Online (Sandbox Code Playgroud)
如果您在git存储库文件夹中,这将打印1.
这个答案提供了一个示例 POSIX shell 函数和一个使用示例来补充@jabbie 的答案。
is_inside_git_repo() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1
}
Run Code Online (Sandbox Code Playgroud)
git返回ERRORLEVEL0如果它是一个Git仓库里面,否则返回错误级别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
Run Code Online (Sandbox Code Playgroud)