[英]How does git tell the remote repository the time of the commit?
If you look at times of each commit on GitHub, and then run git log
, you will see that the times of the commits between the remote and local repos are the exact same, even if you ran git push
a few minutes after git commit
. git 如何告诉存储库所述提交的时间?
只要您为该提交运行git commit
,就会记录时间。 推送仅使用您的本地提交更新 github,因此不会保留推送时间。
编辑:如果您询问具体机制,时间只是随着提交以及更改、您的消息和作者/贡献者一起传递。
让我们看看一个实际的提交。 这是 Git 存储库中 Git 本身的提交:
$ git cat-file -p HEAD | sed 's/@/ /'
tree 8f78544663a8f3e9e3d4ee9125cf54a863498bfb
parent b4e245a17adc1b336269f7350e88eede6e713157
author Junio C Hamano <gitster pobox.com> 1605735205 -0800
committer Junio C Hamano <gitster pobox.com> 1605735205 -0800
Sixth batch
Signed-off-by: Junio C Hamano <gitster pobox.com>
任何提交的组成部分是:
tree
线间接存储的;author
行中看到一些元数据,在提交者行中看到一些元数据。 元数据包括提交的父级的原始 hash ID; 在这里,有一位父母,其 hash ID 出现在(单) parent
行上。 元数据可以包括额外的 header 行,但在这里,在标准tree
、 parent
、 author
和committer
之后,我们只看到主题( Sixth batch
)和正文,由第一组 header 行之后的空行隔开。
(间接存储tree
的一个原因是 Git 然后可以重用它。例如,不更改任何内容的合并提交,或撤消前一个提交的还原,将创建一个与前一个快照完全匹配的新快照。在这里,Git 可以存储新提交,而无需添加新提交以外的任何内容 object。)
正如kevin-robb 所说,日期和时间戳是提交本身的一部分。 这些编码有点奇怪:它们采用自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数形式。 在这里,从那时起是1605735205
秒,或者Wed Nov 18 21:33:25 UTC 2020
。 我们还得到一个时区偏移量: -0800
。 这以小时和分钟表示,十进制,每个两位数字,因此偏移量是格林威治以西 8 小时或太平洋标准时间。 从晚上 9:33 开始花费 8 小时,我们得到了下午 1:33,这确实是git log
显示的内容:
$ git log -n 1 | sed 's/@/ /'
commit faefdd61ec7c7f6f3c8c9907891465ac9a2a1475
Author: Junio C Hamano <gitster pobox.com>
Date: Wed Nov 18 13:33:25 2020 -0800
Sixth batch
Signed-off-by: Junio C Hamano <gitster pobox.com>
因为上面是并且将永远是hash ID 为faefdd61ec7c7f6f3c8c9907891465ac9a2a1475
的提交的确切数据,该提交的时间戳必须是我们刚刚看到的值:
$ (printf 'commit 279\0'; git cat-file -p HEAD) | shasum
faefdd61ec7c7f6f3c8c9907891465ac9a2a1475 -
(279 是提交数据中的字节数,由git cat-file -p HEAD | wc -c
找到)。
如果我们接受这个提交并将其更改为保存一些其他日期和时间戳,并通过 Git 的 object 哈希器运行它,我们将获得一些其他哈希 - 一个其 hash ID不是faefdd61ec7c7f6f3c8c9907891465ac9a2a1475
的提交提交faefdd61ec7c7f6f3c8c9907891465ac9a2a1475
的日期为1605735205 -0800
。 因此,任何具有提交faefdd61ec7c7f6f3c8c9907891465ac9a2a1475
的 Git 存储库都具有该日期和时间,没有其他。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.