GitHub - 指定时间的存储库状态

问题描述 投票:0回答:4

我是使用 git 版本控制工具的初学者。

我想在指定时间(例如 2013 年 10 月 5 日)下载存储库状态(文件)。 我怎样才能做到这一点?

git github
4个回答
24
投票

截至 2019 年 5 月(不确定何时引入),您只需按以下格式添加日期 -

HEAD@{2019-04-29}
- 在提交通常所属的 URL 位置。

示例:这是最近提交的 React 存储库的链接 https://github.com/facebook/react/tree/95e06ac3d091b6746be80d13840884dc7894b01c

将提交哈希替换为上述格式的日期 https://github.com/facebook/react/tree/HEAD@{2019-04-29} 将带您进入 4 月 29 日的 React 存储库。

您还可以根据时间比较提交 - 请在此处查看 Github 帮助文档的更多信息:https://help.github.com/en/articles/comparing-commits-across-time#comparisons-across-time

十一月更新2021: 正如下面的评论中提到的,现在似乎对您可以返回的天数有限制(根据我的经验,大约为 134 天)。

引用的 Github 帮助文章似乎也删除了任何有关时间比较的提及。

2024 年 3 月更新: 根据评论,似乎又可以工作了。


5
投票

单击“提交”并查找该日期的提交。现在将存储库克隆到您的计算机并检查该提交。


1
投票

使用

rev-list
命令搜索按时间顺序颠倒的提交

git rev-list <branch-name> --max-count=1 --before=<timestamp>

举个具体的例子。 2014 年 1 月 10 日之前在我的 master 分支上的提交

git rev-list master --max-count=1 --before=2014-10-01

1
投票

首先,我强烈建议您避免使用日期。这是在某个时间点查看存储库的一种非常不精确的方式。相反,要准确,并选择特定的提交或标签。通常人们希望恢复到特定版本,在这种情况下应该有一个标签。您可以通过键入

git tag
列出所有可用标签。

如果您更愿意使用提交,请使用

git log
查找提交。此命令支持很多选项,您可以通过键入
git help log
查看这些选项。例如,在 GitHub 上的
o-js
存储库上
git log --oneline
会生成:

10b5421 Add .npmignore to bower.json ignore list.
a7681b2 Update package.json for new CommonJS structure and add an .npmignore.
c9e3a52 Migrated everything to CommonJS format modules (no more hacks for the we
9af7a3d Lots of adjustments to reduce the minified size by several kilobytes.
c1c7bda Better error messaging across the board (no more "..." errors). Fixes #4
008871d Adjust exception throwing a bit to be more minification-friendly.
3bce458 Expose o.ucFirst to the public.
866d53e Added o.positiveIntType due to this type being a common case.
cc395d1 New CHANGES file format.
f26de19 Massage the CHANGES list a bit for the next release.

哈希值实际上有 40 个字符长,但

--oneline
仅包含前 7 个字符,许多其他命令也是如此。您可以使用 40 个字符版本或 7 个字符版本的哈希值。

如果您想列出某个日期前后的提交,您可以这样做:

git log --oneline --since=2013-05-05 --until=2013-05-15

您询问是否能够下载文件。这让我认为您不想克隆存储库,而只是想要存储库在某个点的完整快照(没有

.git
目录)。这是使用
git archive
命令完成的,该命令采用时间点的
tree-ish
值。标签和提交对此效果很好。

如果您想要

o-js
存储库的 tar 存档以供发布
v0.0.6
,您可以这样做:

git archive --output=o-js-0.0.6.tar v0.0.6

这会在当前目录中创建

o-js-0.0.6.tar
,所有文件都处于创建
v0.0.6
标签时的状态。同样,您可以将命令中的
v0.0.6
替换为提交哈希,它也能正常工作。

如果您实际上并不只想要文件,而是想将 git checkout 倒回到某个时间点,您可以这样做:

git reset --hard v0.0.6

git reset
命令对于缺乏经验的人来说是危险的,因此请谨慎使用。这份关于
git reset
的文档是一个很好的开始:

http://git-scm.com/blog/2011/07/11/reset.html

© www.soinside.com 2019 - 2024. All rights reserved.