我正在为zsh编写Git管理脚本。如何检查当前目录是否是Git存储库?(当我不在Git存储库中时,我不想执行一堆命令并得到一堆“fatal: Not a git repository”响应)。
您可以使用:
git rev-parse --is-inside-work-tree
git rev-parse --show-toplevel
是否与我正在检查的子文件夹匹配。 - alperWilliam Pursell
的回答那样做一些类似于if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
的事情? - alper从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;
你可以将其封装在函数中或在脚本中使用。适用于和zsh的一行条件语句。[ -d .git ] && echo .git || git rev-parse --git-dir > /dev/null 2>&1
git rev-parse --is-inside-git-dir
。在设置我的 PS1 之前,我个人使用 git rev-parse --is-inside-work-tree
。 - juliohm--is-inside-git-dir
只有在你实际处于存储库的 .git
目录内部时才会返回真。我认为发帖者不是在寻找这个。 - alexia--is-inside-work-tree
和 --is-inside-git-dir
都无法工作。参见:https://groups.google.com/forum/#!topic/git-users/dWc23LFhWxE。 - fisherwebdev.git
,那么这将失败。为了提高健壮性,请省略[ -d .git ]
并使用git rev-parse ...
。 - Peter John Acklam使用 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)"
来确定当前目录是否为最顶层的目录。
> /dev/null 2>&1
是一个常见的 Linux 命令,用于将标准输出和标准错误输出重定向到 /dev/null
设备文件中,从而防止它们在终端上显示。这通常用于在后台运行命令或脚本时抑制输出。 - M Imam Pratama> /dev/null
将标准输出重定向到位桶。2>&1
将标准错误输出发送到相同的位置。 - William Pursell2> /dev/null
不起作用? - ocodogit rev-parse --is-inside-work-tree
会将“true”或“false”打印到标准输出(stdout)。它会将错误消息(例如“fatal: not a git repository”)打印到标准错误(stderr)。 - William Pursell或者你可以这样做:
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
-e
模式下不要进行冗余操作和作业:[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" == "true" ]
,该命令用于检查当前目录是否为Git仓库。 - ivan_pozdeev[ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]
该操作不包含任何冗余步骤,并在-e
模式下运行。
git rev-parse
是否成功,忽略其输出即可。git
命令都仅在工作树内有效。因此,对于脚本编写目的,您最有可能关心的是不仅在“git repo”内部,而且还在工作树内部。[
。只需执行 if git rev-parse --is-inside-work-tree; then ...
(根据需要进行重定向)。 - William Pursell==
也无效)。 - vinc17is_inside_git_repo() {
git rev-parse --is-inside-work-tree >/dev/null 2>&1
}
git
在Git仓库内返回错误级别0
,否则返回错误级别128
。(如果在Git仓库中,它还会返回true
或false
。)
用法示例
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
内部,它会成功但打印 false
。 - ivan_pozdeevgit rev-parse --is-inside-work-tree
返回 true
或 false
,那么它 就在 git 存储库内,这就是该函数的返回值。也就是说,该函数是正确的。 - go2null不确定是否存在公开可访问/文档化的方法来实现此操作(在git源代码中有一些可以使用/滥用的内部git函数)
你可以尝试如下操作:
if ! git ls-files >& /dev/null; then
echo "not in git"
fi
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
&& [ $? -eq 0 ]
是多余的,特别是如果你已经在一个 if
中了。 - mtraceurgit tag
命令是否有效? - jave.webgit rev-parse 2> /dev/null; [ $? == 0 ] && echo 1
.git
目录内部,此命令也将返回rc 0 -- 这可能是您想要的,也可能不是。 - ivan_pozdeevgit rev-parse 2>&-
。 - jthill[ $? == 0 ]
是多余的;因为 &&
已经隐含了测试。上述命令等同于 git rev-parse 2>/dev/null && echo 1
。 - Deven T. Corzine[ $? == 0 ]
并不完全多余。如果你正在使用支持 ==
的 [
版本,那么它与 git rev-parse 2> /dev/null && echo 1
是完全等价的。但是如果你正在使用不认识 ==
的 [
版本,则这个版本就是一个错误。 - William Pursellgit status >/dev/null 2>&1 && echo Hello World!
git_status="$(git status --porcelain --branch 2> /dev/null)"; if [ $? -eq 0 ]; then echo something with $git_status; fi
- Michael
$GIT_DIR
或$GIT_WORK_TREE
环境变量,以及它们之间的交互。 - o11c