【问题标题】:Searching last committed files in git that matches to given file path在 git 中搜索与给定文件路径匹配的最后提交的文件
【发布时间】:2019-12-15 04:26:56
【问题描述】:

我正在使用下面的 git 命令获取最后提交的文件,

git diff --name-only --relative= --diff-filter=AMDR --cached @~

然后我得到如下输出,

bitbucket-pipelines.yml
app-connect/source/cn/ConnectBuild.sh

现在我想检查“是否有来自app-connect/source/cn/ 路径的文件,那么只有我做一些事情。

例如:

if git diff --name-only --relative= --diff-filter=AMDR --cached @~  | grep "/app-connect/source/cn"; then do something; fi

您能告诉我在这种情况下如何使用 grep 或正则表达式来检查给定路径中的文件是否存在。

【问题讨论】:

    标签: git grep


    【解决方案1】:

    我认为使用 git show 会简单得多

    lines=$( git show --pretty="" --name-only -- app-connect/source/cn | wc -l )
    if [ $lines -gt 0 ]; then
        echo do something
    fi
    

    如果你真的需要使用 grep 来检查路径,不要提供 git 的路径,管道进入 grep,在那里设置正则表达式,然后管道进入 wc

    lines=$( git show --pretty="" --name-only | egrep blahblah | wc -l )
    

    【讨论】:

    • 作为旁注,以防万一您不确定最后一次提交确实在当前签出的分支上,您可以考虑替换 show --pretty="" 部分并使用 git log --all -1 --no-merges --name-only --pretty=format:"" 检查所有分支
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多