46

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

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

2
  • Did you try changing your clock? =) I'd think that should work locally, but not sure what'd happen when others go to merge. Commented Dec 15, 2008 at 2:28
  • 5
    Timestamps aren't relevant to merges. Commented Dec 15, 2008 at 4:31

7 Answers 7

71

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! 

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

Sign up to request clarification or add additional context in comments.

5 Comments

It also works with ISO 8601 date format: "2029-12-19 15:14:05 -0800". I like that.
What is the difference between GIT_AUTHOR_DATE and GIT_COMMITTER_DATE?
@Blago: In git, the author (person who wrote the change) and committer (person who put the change in the repository) are tracked separately. Lets you do all kinds of great things.
"You should wait a bit." LOL
In view of back to the future now being in the past, I'm really looking forward to having had waited long enough ;)
51

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

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

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

4 Comments

Anything up to January 19th 3:14:07 2038 UTC should work. en.wikipedia.org/wiki/Year_2038_problem
Amending a commit only changes the committer date. Git tracks 2 dates: commit and author date. Author date will remain as the original via amend or any other git command that edits commits (eg: rebase and cherry-pick).
@Zombies wrong: git log shows GIT_AUTHOR_DATE by default, not GIT_COMMITTER_DATE. You can check with git log --pretty=fuller which shows both. Amending the commit with the --date option changes the author date. Amending it without this option changes only the commit date, since it re-creates the commit.
This only issue of this elegant solution is that it applies only on the last commit. No way to specify a older commit than the tip of the branch.
10

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

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.

Comments

2

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

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.

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).

1 Comment

your newest | xargs date -u -r dies on spaces in the filename. sed in the FSF repo supports nul-terminated lines and that'd make the whole pipeline safe but until then I'd say use -d\\n when xarg'ing filenames. . . . find . -name .git -prune -o -type d -o -print0 | xargs -0 ls -dt | sed q is shorter
0

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)

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

commit: use timestamp_t for author_date_slab

The author_date_slab is used to store the author date of a commit when walking with the --author-date flag 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.c were converted to timestamp_t in 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.

Comments

0

Maximum future we can go to

Also if you want to have some mega-fun and create a commit with the largest date GitHub will accept:

d="@$(python -c 'print(2**63 - 1)') +0000" GIT_COMMITTER_DATE="$d" git commit -m 'max future' --date "$d" 

The format @<number> +0000 create a UNIX timestamp, which is natively how commit dates are stored in the git commit object. It is a 64-bit integer, but if we go over 2**63 - 1 GitHub complains about overflow issues:

remote: error: object a9b376702620cc9a165ba4b5d3599286006b7798: badDateOverflow: invalid author/committer line - date causes integer overflow

Here's a commit at that date on GitHub:

Dec 4, 292277026596

The Futurama time travel song comes to mind.

Tested on Git 2.40.1, Ubuntu 23.10.

Comments

-5

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.

1 Comment

Good idea. In Unix like systems you could use the "at" command (see "man at" for usage).