【问题标题】:Programmatic git status程序化 git 状态
【发布时间】:2010-10-10 14:30:47
【问题描述】:

有谁知道 git 管道(绝对不是瓷器)确定是否:

  • 自上次提交以来,repo 中有一些编辑,并且
  • 本地 HEAD 是否领先于 origin/HEAD

我希望以编程方式确定这一点,因此不想用瓷器和各种 sed-fu 来解决这个问题。

【问题讨论】:

    标签: git


    【解决方案1】:

    更新:作为 toupeiramentioned below,您可以使用 git status--porcelain 选项(自 2009 年 9 月提交 6f15787,git 1.7.0 以来)。

    我在回答“What does the term porcelain mean in Git?”中提到:

    也许--porcelain这里的意思是“产生适合瓷脚本消费的输出”

    但是,这不会显示前后信息:请参阅“What to add to “git status --porcelain” to make it behave like “git status”?”:为此,您仍然需要使用其他命令:请参阅“How to know if git repository has changes that have not been synchronized with server?


    2009 年 3 月的初步回答

    在瓷器命令中,a:

    $ git diff HEAD
    

    为您提供自上次提交以来的更改(如果您运行“git commit -a”,您将提交什么)。

    管道命令中可能的等价物是:

    $ git ls-files -m
    

    用于列出所有修改的(工作目录或索引)文件


    如果您通过克隆他人的存储库来创建存储库,则远程“master”分支将复制到名为“origin”的本地分支。您将获得自己的“主”分支,该分支未绑定到远程存储库。

    总有一个当前的头,称为 HEAD。 (这实际上是一个符号链接,.git/HEAD,指向像 refs/heads/master 这样的文件。)

    运行“git status”并分析输出:

    # On branch master
    # Your branch is ahead of 'origin/master' by 11 commits.
    #
    

    SO 问题“Why is Git telling me “Your branch is ahead of ‘origin/master’ by 11 commits.” and how do I get it to stop?”中的更多详细信息

    管道命令中的可能等效项:

    * git-for-each-ref
    

    用于列出所有提交,但也需要分析输出...

    再说一遍,git ls-files could be used to produced the same result than a git status

    git ls-files --exclude-per-directory=.gitignore --exclude-from=.git/info/exclude \
                        --others \
                        --modified \
                        -t
    

    【讨论】:

    • 太好了。非常感谢。
    • "git ls-files --exclude-standard ..." 会简单一些。
    【解决方案2】:

    git status 现在有一个用于脚本目的的--porcelain 参数(以及用于机器解析的替代-z),这优于git ls-files,它不显示添加到索引的文件。

    【讨论】:

    • 来自the documentation for --porcelain:“以易于解析的格式为脚本提供输出。这类似于短输出,但无论用户配置如何,都会在 git 版本之间保持稳定。”
    • 不过,发行说明中并不清楚何时添加了 --porcelain 标志。
    • 我找到了!我用 git log --grep "--porcelain" --date-order 搜索了 Git 日志,他们显示该标志是在 2009 年 9 月 5 日的提交 6f15787181a163e158c6fee1d79085b97692ac2f 中添加的。顺便说一下,git tag --contains 6f15787 | sort -V | less 表明这实际上直到 v1.7.0 才以稳定版本发布, 在Febuary 12th, 2010.
    猜你喜欢
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2016-08-25
    相关资源
    最近更新 更多