简体   繁体   English

如何将最新的提交日期更改为最旧的?

[英]How to change the latest commit date to oldest?

So I'm new to github.所以我是 github 的新手。 I have committed my readme.md file 3 days ago, but I found out that there was a mistake on my readme.md file.我在 3 天前提交了我的 readme.md 文件,但我发现我的 readme.md 文件有错误。 I want to edit the file, but if I edit it, the commit date will change to the current timestamp.我想编辑文件,但如果我编辑它,提交日期将更改为当前时间戳。 Is there any way that I can edit the file without changing the commit date to the current timestamp?有什么方法可以在不将提交日期更改为当前时间戳的情况下编辑文件? I'd like to keep the first commit date as I commit changes.我想在提交更改时保留第一个提交日期。

So I'm new to github.所以我是 github 的新手。 I have committed my readme.md file 3 days ago, but I found out that there was a mistake on my readme.md file.我在 3 天前提交了我的 readme.md 文件,但我发现我的 readme.md 文件有错误。 I want to edit the file, but if I edit it, the commit date will change to the current timestamp.我想编辑文件,但如果我编辑它,提交日期将更改为当前时间戳。

That's sort of right, but also sort of wrong.这有点对,但也有点不对。 Most importantly, it suggests that you're thinking of Git as storing files—but Git doesn't store files.最重要的是,它表明您将 Git 视为存储文件——但 Git 不存储文件。 Git stores commits . Git 存储提交

The thing that has a date is not the file, it's the commit .有日期的不是文件,而是提交 The commit then contains files , but every commit contains every file .然后提交包含文件,但每次提交都包含每个文件 So if you make a new commit now with the old readme.md file in it, that new commit also has the current time as its timestamp.因此,如果您现在使用旧的readme.md文件进行新提交,则该新提交将当前时间作为其时间戳。 It's just that the new copy of readme.md is the old copy of readme.md .这只是新副本readme.md旧副本readme.md Those two files have the same contents, so Git, which de-duplicates files based on content, stores the content only once.这两个文件具有相同的内容,因此基于内容对文件进行重复数据删除的 Git 只存储一次内容。

If you assign the commit dates to the file, this means the file now has two separate dates.如果您将提交日期分配给文件,这意味着文件现在有两个单独的日期。 In fact, of course, the commit dates are assigned to the commits .事实上,当然,提交日期是分配给commits 的 There are two different commits, with two different dates.有两个不同的提交,有两个不同的日期。 The two different commits may have the same (shared) readme.md , or may have two different readme.md files.两个不同的提交可能具有相同(共享)的readme.md ,或者可能具有两个不同的readme.md文件。

Is there any way that I can edit the file without changing the commit date to the current timestamp?有什么方法可以在不将提交日期更改为当前时间戳的情况下编辑文件?

Whenever you make a new commit, it gets a new commit timestamp.每当您进行新提交时,它都会获得一个新的提交时间戳。 In fact, though, it gets two new commit timestamps.但事实上,它获得了两个新的提交时间戳。 One of these two is very easy to control because you can use --date= date to give it a particular date-and-time-stamp.这两者之一非常容易控制,因为您可以使用--date= date为其指定特定的日期和时间戳。 The other is harder to control: it always defaults to "now".另一个更难控制:它总是默认为“现在”。

So, if you like, you can easily set one of the two dates to any value you want.因此,如果您愿意,可以轻松地将两个日期之一设置为您想要的任何值。 The one you can set so easily, using a command-line argument, is the author date .您可以使用命令行参数轻松设置的一个是作者日期 The other date is the one that Git calls the committer date and there are reasons to avoid setting it out of order, 1 but Git will allow you to override it if you are sufficiently determined.另一个日期是 Git 称为提交者日期的日期,并且有理由避免将其设置为乱序1,但如果您有足够的决心,Git 将允许您覆盖它。 Simply set the environment variable GIT_COMMITTER_DATE before running git commit —it takes the same string values as the --date= option—and Git will believe that your computer's clock is set to that value and will therefore use that as the commit timestamp.只需在运行git commit之前设置环境变量GIT_COMMITTER_DATE它采用与--date=选项相同的字符串值--date=会相信你的计算机的时钟设置为该值,因此将使用它作为提交时间戳。

There is rarely a good reason to do this kind of date manipulation.很少有充分的理由进行这种日期操作。 The fact is that files evolve, including README-type files, and if you fix one, it's just like fixing any other bug.事实是文件不断发展,包括 README 类型的文件,如果您修复一个,就像修复任何其他错误一样。 The file was wrong in the old commits, and is corrected in this commit and each subsequent commit that also contains the file.该文件在旧提交中是错误的,并在此提交和每个包含该文件的后续提交中得到纠正。 Tools like git blame will find the commit that fixed the specific fixed line(s), and will keep looking back in history for the author of previous lines.诸如git blame类的工具会找到修复特定固定行的提交,并会不断回溯历史以查找前几行的作者。


1 One reason is that it will confuse humans. 1原因之一是它会混淆人类。 A more technical reason is less describable and usually not all that interesting anyway: for particularly large commit graphs, Git is growing some optimizations these days that involve adding auxiliary graph data, where out-of-order committer dates make things more complicated and potentially slower.一个更技术性的原因不太好描述,而且通常也不是那么有趣:对于特别大的提交图,Git 最近正在增加一些优化,包括添加辅助图数据,其中乱序提交者日期使事情变得更加复杂并且可能更慢.

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

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