且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检查当前目录是否为Git存储库

更新时间:2023-11-25 16:56:22

从bash完成文件中复制以下内容是一种天真的做法

 #Copyright(C)2006,2007 Shawn O. Pearce< spearce@spearce.org> 
#概念上基于gitcompletion(http://gitweb.hawaga.org.uk/)。
#在GNU通用公共许可证2.0版下发布。

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

您可以将它封装在函数中,或者在脚本中使用它。



压缩成适用于bash或zsh的单行条件

  [-d .git] || git rev-parse --git-dir> / dev / null 2>& 1 


I am writing a series of scripts for Git management in zsh.

How do I check if the current directory is a Git repository? (When I'm not in a Git repo, I don't want to execute a bunch of commands and get a bunch of fatal: Not a git repository responses).

Copied from the bash completion file the following is a naive way to do it

# 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;

You could either wrap that in a function or use it in a script.

Condensed into a one line condition suitable for bash or zsh

[ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1