简体   繁体   English

git 如何告诉远程仓库提交的时间?

[英]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 . 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 . How does git tell the repository the time of the said commit? git 如何告诉存储库所述提交的时间?

The time is logged as soon as you run git commit for that commit.只要您为该提交运行git commit ,就会记录时间。 The push only updates github with your local commits, and as such the time of the push is not kept.推送仅使用您的本地提交更新 github,因此不会保留推送时间。

Edit: If you are asking about the specific mechanism, the time is simply passed with the commit along with the changes, your message, and the author(s)/contributor(s).编辑:如果您询问具体机制,时间只是随着提交以及更改、您的消息和作者/贡献者一起传递。

Let's look inside an actual commit.让我们看看一个实际的提交。 Here is a commit from the Git repository for Git itself:这是 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>

The components of any commit are:任何提交的组成部分是:

  • a full snapshot of every file: this is stored indirectly through the tree line;每个文件的完整快照:这是通过tree线间接存储的;
  • metadata, including the name of the person who wrote the commit: here, we see some of that metadata on the author line, and some of it on the committer` line.元数据,包括编写提交的人的姓名:在这里,我们在author行中看到一些元数据,在提交者行中看到一些元数据。

The metadata include the raw hash ID of the parent(s) of the commit;元数据包括提交的父级的原始 hash ID; here, there is one parent, whose hash ID appears on the (single) parent line.在这里,有一位父母,其 hash ID 出现在(单) parent行上。 The metadata can include additional header lines, but here, after the standard tree , parent , author , and committer , we see only the subject ( Sixth batch ) and body, set off by a blank line after the first set of header lines.元数据可以包括额外的 header 行,但在这里,在标准treeparentauthorcommitter之后,我们只看到主题( Sixth batch )和正文,由第一组 header 行之后的空行隔开。

(One reason for storing the tree indirectly is that Git can then re-use it. For instance, a merge commit that changes nothing, or a revert that undoes the immediately previous commit, will make a new snapshot that exactly matches the previous snapshot. Here, Git can store the new commit without adding anything other than the new commit object.) (间接存储tree的一个原因是 Git 然后可以重用它。例如,不更改任何内容的合并提交,或撤消前一个提交的还原,将创建一个与前一个快照完全匹配的新快照。在这里,Git 可以存储新提交,而无需添加新提交以外的任何内容 object。)

The date-and-time-stamp are part of the commit itself, just as kevin-robb said .正如kevin-robb 所说,日期和时间戳是提交本身的一部分。 These are encoded a bit oddly: they take the form of a number of seconds since 1 Jan 1970 00:00:00 UTC.这些编码有点奇怪:它们采用自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数形式。 Here, that's 1605735205 seconds since then, or Wed Nov 18 21:33:25 UTC 2020 .在这里,从那时起是1605735205秒,或者Wed Nov 18 21:33:25 UTC 2020 We also get a time-zone-offset: -0800 .我们还得到一个时区偏移量: -0800 This is expressed in hours and minutes, in decimal, with two digits for each, so the offset is 8 hours west of Greenwich, or Pacific Standard Time.这以小时和分钟表示,十进制,每个两位数字,因此偏移量是格林威治以西 8 小时或太平洋标准时间。 Taking 8 hours from 9:33 PM, we get 1:33 PM, which is indeed what git log shows:从晚上 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>

Because the above is and will always be the exact data for the commit whose hash ID is faefdd61ec7c7f6f3c8c9907891465ac9a2a1475 , the time stamp of that commit must be the value we just saw:因为上面并且将永远是hash ID 为faefdd61ec7c7f6f3c8c9907891465ac9a2a1475的提交的确切数据,该提交的时间戳必须是我们刚刚看到的值:

$ (printf 'commit 279\0'; git cat-file -p HEAD) | shasum
faefdd61ec7c7f6f3c8c9907891465ac9a2a1475  -

(279 is the number of bytes in the commit data, as found by git cat-file -p HEAD | wc -c ). (279 是提交数据中的字节数,由git cat-file -p HEAD | wc -c找到)。

If we take this commit and alter it to hold some other date-and-time stamp, and run it through Git's object hasher, we'll get some other hash—a commit whose hash ID is not faefdd61ec7c7f6f3c8c9907891465ac9a2a1475 .如果我们接受这个提交并将其更改为保存一些其他日期和时间戳,并通过 Git 的 object 哈希器运行它,我们将获得一些其他哈希 - 一个其 hash ID不是faefdd61ec7c7f6f3c8c9907891465ac9a2a1475的提交Commit faefdd61ec7c7f6f3c8c9907891465ac9a2a1475 will be dated 1605735205 -0800 .提交faefdd61ec7c7f6f3c8c9907891465ac9a2a1475的日期为1605735205 -0800 So any Git repository that has commit faefdd61ec7c7f6f3c8c9907891465ac9a2a1475 has it with that date and time and no other.因此,任何具有提交faefdd61ec7c7f6f3c8c9907891465ac9a2a1475的 Git 存储库都具有该日期和时间,没有其他。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM