git commit - 将时间戳设置为未来

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/367262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:07:23  来源:igfitidea点击:

git commit - setting timestamps into the future

git

提问by Paul Pladijs

I need to make some commits using Git but I would like the timestamp in git-log to be in the future.

我需要使用 Git 进行一些提交,但我希望 git-log 中的时间戳是将来的。

How can I do a commit in git that causes a future timestamp to register in the git-log?

如何在 git 中进行提交以在 git-log 中注册一个未来的时间戳?

回答by Dustin

You should wait a bit.

你应该稍等一下。

Or you can do this:

或者你可以这样做:

/tmp/x 604% env GIT_AUTHOR_DATE='Wed Dec 19 15:14:05 2029 -0800' git commit -m 'future!'
[master]: created 6348548: "Future!"
 1 files changed, 1 insertions(+), 0 deletions(-)

/tmp/x 605% git log 

Author: Dustin Sallings <[email protected]>
Date:   Wed Dec 19 15:14:05 2029 -0800

    Future!

Notethat there's both an author date and a committer date, so be sure to set the right one (or both).

请注意,有作者日期和提交者日期,所以一定要设置正确的一个(或两者)。

回答by Paul Pladijs

You can amend the commit, an example with the year 2037:

您可以修改提交,以 2037 年为例:

git commit --amend --date="Wed Feb 16 14:00 2037 +0100"

I tried the year 2038 too but then I got a nullvalue for the date.

我也试过 2038 年,但后来我得到了一个值。

回答by Hugo

If you want to retain an actual change-date when adding a project to git, you can do so with

如果要在向 git 添加项目时保留实际更改日期,可以使用

env GIT_AUTHOR_DATE="`ls -rt *.cpp|tail -1|xargs date -u -r`" git commit -m "Old sources retaining old change-dates of last changed
 file: `ls -rt *.cpp|tail -1`, actual commit date: `date`"

This will commit with the change-date of the last-changed *.cpp-file, and a nice explaining message of the actual commit date.

这将提交上次更改的 *.cpp 文件的更改日期,以及实际提交日期的一个很好的解释消息。

回答by Dan Hunsaker

By combining Hugo's answer (1) with information found over here (2), and tossing in some sed, I got this:

通过将 Hugo 的答案 ( 1) 与此处找到的信息 ( 2) 结合起来,并投入一些sed,我得到了这个:

alias newest="find . -path ./.git -prune -o -type f -exec stat -c \"%y %n\" '{}' + | sort -r | head -1 | sed s#'.*\./'##"
GIT_AUTHOR_DATE="$(newest | xargs date -u -r)" GIT_COMMITTER_DATE="$(newest | xargs date -u -r)" git commit -m "Old sources retaining old change-dates of last changed file: $(newest), actual commit date: $(date)"

The main difference is that this version does a recursive search, so you get the latest file anywhere in the tree - though it does skip the .git directory, intentionally.

主要区别在于此版本执行递归搜索,因此您可以在树中的任何位置获取最新文件 - 尽管它确实有意跳过了 .git 目录。

You may, of course, want to drop one of the date variables here, and I am using a fairly recent version of bash (4.2.37(1)-release), so the $() notation might not work for you (just replace it with backticks (`) instead).

当然,您可能想在此处删除日期变量之一,而我使用的是相当新版本的 bash(4.2.37(1)-release),因此 $() 表示法可能不适合您(只是用反引号 (`) 代替)。

回答by VonC

If you want to amend a commit with a date "from the future" as in this answer:

如果您想修改带有“来自未来”日期的提交,如本答案所示

git commit --amend --date="Wed Feb 16 14:00 2037 +0100"
  • not use a date after 2016 (Git 2.18 or less)
  • use Git 2.19 (Q3 2018)
  • 不使用 2016 年之后的日期(Git 2.18 或更低版本)
  • 使用 Git 2.19(2018 年第三季度)

See commit 1820703(21 Aug 2018) by Derrick Stolee (derrickstolee).
(Merged by Junio C Hamano -- gitster--in commit 1392c5d, 27 Aug 2018)

请参阅Derrick Stolee ( ) 的提交 1820703(2018 年 8 月 21 日(由Junio C Hamano合并-- --commit 1392c5d,2018 年 8 月 27 日)derrickstolee
gitster

commit: use timestamp_tfor author_date_slab

The author_date_slabis used to store the author date of a commit when walking with the --author-dateflag in rev-list or log.
This was added as an 'unsigned long' in 81c6b38("log: --author-date-order", June 2013, Git 1.8.4-rc0)

Since 'unsigned long' is ambiguous in its bit-ness across platforms (64-bit in Linux, 32-bit in Windows, for example), most references to the author dates in commit.cwere converted to timestamp_tin dddbad7("timestamp_t: a new data type for timestamps", April 2017, Git 2.14.0-rc0)

However, the slab definition was missed, leading to a mismatch in the data types in Windows.
This would not reveal itself as a bug unless someone authors a commit after February 2106, but commits can store anything as their author date.

commit:timestamp_t用于author_date_slab

author_date_slab使用--author-daterev-list 或 log 中的标志时,用于存储提交的作者日期。
这是在81c6b38(“ log: --author-date-order”,2013 年 6 月,Git 1.8.4-rc0)中作为“无符号长”添加的

由于“ unsigned long”是在跨平台其位岬暧昧(64位在Linux中,在Windows 32位,例如),以在提交日期最引用commit.c被转换成timestamp_tdddbad7(“timestamp_t:一个新的数据类型为时间戳”,2017 年 4 月,Git 2.14.0-rc0)

但是,slab 定义被遗漏,导致 Windows 中的数据类型不匹配。除非有人在 21062 月之后提交提交,否则
这不会显示为错误,但提交可以将任何内容存储为他们的作者日期。

回答by msingleton

May I ask why you would want to do this?

我可以问你为什么要这样做吗?

If you don't want to change your clock, I would suggest creating a script to do the commit and use the Windows Scheduler (or whatever equivalent for your OS) to run the script at the time you want the commit to be.

如果您不想更改时钟,我建议您创建一个脚本来执行提交并使用 Windows 调度程序(或您的操作系统的任何等效程序)在您希望提交的时间运行脚本。