【问题标题】:Does github remember commit IDs?github 记得提交 ID 吗?
【发布时间】:2015-05-11 13:44:36
【问题描述】:

几天来,我正在为 Scrollback 项目重写 install.sh 文件,因为我是唯一一个在本地工作并在本地完成的人,所以我不断提交修改相同的提交,偶尔推送到我的 fork掌握。 (请忽略这里的最佳实践,我是一个人工作的)。

在这期间我记得给某人发了一封电子邮件,显示我完成了一半的工作,网址为https://github.com/sindhus/scrollback/blob/8d8f7f414383499c2ab6fec586b4c9665a41c7aa/install.sh

现在由于一些困惑,我在本地失去了工作(想想 rm -rf),我记得在此之前推动。所以 github 在某些时候确实看到了我的 install.sh 的 rebase 提交 ID。

如您所见,上面的 URL 允许我通过提交 ID 访问此 blob。 但是我无法在本地访问它,因为同一个 repo 被强制推送。

我的问题是如何让 github 向我显示文件的所有提交 ID?无论路径如何,它可能知道该文件的所有 ID。如果我必须使用他们的 API,我不介意,但我想要一些想法来深入研究。

谢谢!

【问题讨论】:

    标签: git github


    【解决方案1】:

    我不确定是否有一种方法可以在修改后的提交中获取文件的所有版本。但是,reflog 将包含有关早期信息的信息,您可以手动提取它们。下面是一个例子。

    这是我的第一次提交

    echo "a" > a.txt && git add a.txt && git commit -m "Version 0"
    

    在那之后,又做了一些修改。

    % echo "aa" > a.txt && git add a.txt && git commit --amend -m "Version 1"
    % echo "aaa" > a.txt && git add a.txt && git commit --amend -m "Version 2"
    % echo "aaaa" > a.txt && git add a.txt && git commit --amend -m "Version 3"
    % echo "aaaaa" > a.txt && git add a.txt && git commit --amend -m "Version 4"
    

    虽然我的日志只有一个条目

    % git log --oneline a8d6c39 第 4 版

    我的 reflog 拥有一切

    % git reflog master
    a8d6c39 master@{0}: commit (amend): Version 4
    cf87b8f master@{1}: commit (amend): Version 3
    c45a91e master@{2}: commit (amend): Version 2
    63c7f5a master@{3}: commit (amend): Version 1
    f2b3336 master@{4}: commit (initial): Version 0
    

    所以,如果您想查看您的文件在版本 4、版本 3 等中的样子,您可以这样做

    % git show a8d6c39:a.txt
    aaaaa
    % git show cf87b8f:a.txt
    aaaa
    % git show c45a91e:a.txt
    aaa
    % git show 63c7f5a:a.txt
    aa
    % git show f2b3336:a.txt
    a
    

    不过,一般来说,即使您是唯一的开发人员,不断修改的“过程”也很糟糕。这是你应该做的一件事情来修复最后一次提交的错误。

    【讨论】:

      【解决方案2】:

      我的问题是如何让 github 向我展示一个文件的所有提交 ID

      如果您偶尔强制推送 (git push --force) 修改后的提交,则该提交 8d8f7 已被 more recent commit with a different SHA 替换。

      这意味着 8d8f7 现在仅在 GitHub 存储库的 reflog 中引用,只有 GitHub 支持才能让您访问
      克隆 repo 不会在该克隆 repo 的本地历史记录中包含 8d8f7。


      GitHub “reflog”:从 GitHub 事件 API 推送事件

      实际上OP sindhusin the comments 指向John Engelman 的“Recovering a commit from Github’s Reflog”:

      GitHub Events API 允许浏览最近的事件:

      curl https://api.github.com/repos/<user>/<repo>/events
      

      “pushEvent”是要查找的。​​p>

      然后可以直接在 GitHub 上创建一个分支,以使该提交再次可见(因为不再悬空,而是由诸如分支之类的实际对象引用):

      curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"ref":"refs/heads/D-commit", "sha":"384f275933d5b762cdb27175aeff1263a8a7b7f7"}' https://api.github.com/repos/<user>/<repo>/git/refs
      
      # JSON request
      {
        "ref": "refs/heads/D-commit",
        "sha": "384f275933d5b762cdb27175aeff1263a8a7b7f7"
      }
      

      你可以need to use a token for the authentication

      【讨论】:

      • 谢谢你,你理解的问题是对的。有没有办法通过他们的 API 获取 github repo 的 reflog?
      • @sindhus 很棒的发现!我已将其包含在答案中以获得更多可见性,并带有其他链接。
      • 我的保护者!在git push --mirror >_ 之后恢复了已删除的分支
      【解决方案3】:

      我并没有真正得到这个问题,所以有一些解决方案:

      查看特定文件的所有提交: git log --follow filename.

      要结帐到旧版本,请找到答案here

      【讨论】:

        【解决方案4】:

        在本地克隆您的 repo,然后在您的文件上尝试:

        git log --follow install.sh
        

        它应该显示您可以在 github 上使用的 ID。

        【讨论】:

          猜你喜欢
          • 2012-01-11
          • 2018-02-01
          • 2018-11-04
          • 2011-06-14
          • 2015-09-04
          • 2019-08-16
          • 2017-05-09
          • 2012-07-24
          • 2023-01-22
          相关资源
          最近更新 更多