【问题标题】:How to get the last commit ID of a remote repo using curl-like command?如何使用类似 curl 的命令获取远程仓库的最后提交 ID?
【发布时间】:2013-10-11 03:57:19
【问题描述】:

我想获取 remote git repo 的最后一次提交 ID。

git rev-parse HEAD 命令适用于本地克隆的 git 存储库,但我想通过 CURL 命令左右从原始 GIT 存储库中获取它。

例如:我想获取 git URL https://git.appfactorypreview.wso2.com/history/apiapp.git/ 的最后一次提交 ID。

怎么做?

【问题讨论】:

    标签: git git-commit


    【解决方案1】:

    试试这个命令

    git log --format="%H" -n 1
    

    【讨论】:

    • 你可以通过git log --format="%H" -n 1移除管道
    • git log -n1 --format="%h" 将提供缩写的提交哈希。
    • 这是错误的。在 git 2.1.4 "%H" 显示本地提交 ID,而不是远程。
    • 你在哪里指定了远程 URL?
    • 问题确实被修改了,但只是为了便于阅读。 OP 明确表示他没有本地克隆,他想使用类似curl 的解决方案。
    【解决方案2】:

    我想你想要的是这样的:

    git ls-remote $URL HEAD
    

    如果远程存储库中不存在HEAD,那么您可能需要:

    git ls-remote $URL refs/heads/master
    

    请注意,在第一种情况下,HEAD 将指向默认分支以在存储库中签出。您需要确保这是您想要的分支,或者只使用第二种形式并指定您想要的分支(将 refs/heads/master 替换为您想要的分支名称:refs/heads/BRANCH_NAME

    【讨论】:

    • 你不能使用HEAD,因为它是一个指向当前分支的指针。但在一个裸仓库中,它不存在HEAD
    • 从不存在是不正确的。案例和要点:git ls-remote git://github.com/jszakmeister/vimfiles.git HEAD。在一个裸仓库中,它告诉 Git 哪个分支作为默认分支检出。确实,你不能指望它存在。因此,在这种情况下,您应该使用适当的 refname。我会更新我的答案。
    【解决方案3】:

    另一种方式,不使用 git log:

    git rev-parse HEAD

    【讨论】:

      【解决方案4】:

      您可以为此使用git ls-remote。因为我得到了一个'Unauthorized access for repository apiapp.git',所以我以 torvalds linux-repo 为例。

      $ git ls-remote --heads git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
      6d15ee492809d38bd62237b6d0f6a81d4dd12d15        refs/heads/master
      

      【讨论】:

        【解决方案5】:

        最后一个提交 ID 的短哈希值更易于人类阅读(阅读:用户友好)。对于后代,有两种方法可以获取最后一次提交 id 的短哈希:

        git rev-parse --short HEAD
        

        git log -n1 --format="%h"
        

        【讨论】:

          【解决方案6】:

          我使用的最简单方法:

          git rev-parse origin/develop
          

          【讨论】:

            【解决方案7】:

            我的回答对 OP 没有帮助,因为他不在 github 上,但我想我还是会提到它,因为它按照 OP 的要求使用 curlwget

            wget -qO- http://api.github.com/repos/Ghini/ghini.desktop/commits/ghini-1.0

            Ghini 是我的仓库,ghini.desktop 是我的仓库,ghini-1.0 是我感兴趣的分支。替换它们以适合您的情况。

            JSON 答案是字典,OP 对其sha 字段感兴趣,但它包含更多信息。

            【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-01-22
            • 2013-09-22
            • 2011-11-17
            • 2011-08-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多