【问题标题】:Check if file exists in any directory in path or if current directory is in a git project检查文件是否存在于路径中的任何目录中,或者当前目录是否在 git 项目中
【发布时间】:2018-07-11 00:33:43
【问题描述】:

我正在向我的.zshrc 文件添加一个短命令,该命令将调用clearlsgit status。仅当当前目录是 git 项目的一部分时,才应调用 git status 命令。

到目前为止,我有一个可以工作的版本,但前提是 .git 文件实际上在当前目录中,所以只能在项目的根目录中。看起来像这样:

alias cl="clear && ls && if [ -e .git ]
then
    git status
fi"

我想知道是否有一种方法可以检查当前目录的任何父目录中是否存在 .git 文件,或者是否有其他方法可以完成此操作。

谢谢!

【问题讨论】:

    标签: bash git shell zsh


    【解决方案1】:

    您无需检查是否存在.git 目录。 Git已经为你做了这些。如果您想知道当前目录是否是 git 存储库的一部分,只需运行:

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

    退出代码会告诉你你的目录是否是 git 存储库的一部分。这使您的别名看起来像:

    alias cl="clear && ls && if git rev-parse --is-inside-work-tree 2> /dev/null
    then
      git status
    fi" 
    

    【讨论】:

    • 我会注意到,这将是一个函数而不是别名更具可读性,并且将以相同的方式调用。
    猜你喜欢
    • 1970-01-01
    • 2022-11-01
    • 2012-03-19
    • 1970-01-01
    • 2013-04-07
    • 2011-04-29
    • 1970-01-01
    • 2013-04-24
    • 2011-01-11
    相关资源
    最近更新 更多