45

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?

bstpierre
  • 30,042
  • 15
  • 70
  • 103

6 Answers6

70

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 <dustin@spy.net>
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).

Dustin
  • 89,080
  • 21
  • 111
  • 133
  • 7
    It also works with ISO 8601 date format: "2029-12-19 15:14:05 -0800". I like that. – sunny256 Sep 02 '09 at 21:00
  • 3
    What is the difference between GIT_AUTHOR_DATE and GIT_COMMITTER_DATE? – Blago Oct 29 '11 at 00:46
  • 4
    @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. – Dustin Oct 30 '11 at 05:16
  • 7
    "You should wait a bit." LOL – JuanPablo Dec 01 '14 at 17:41
  • In view of _back to the future_ now being in the past, I'm really looking forward to having had waited long enough ;) – Sebb Oct 22 '15 at 21:49
50

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.

Paul Pladijs
  • 18,628
  • 5
  • 28
  • 31
  • 12
    Anything up to January 19th 3:14:07 2038 UTC should work. http://en.wikipedia.org/wiki/Year_2038_problem – Arrowmaster Feb 16 '11 at 20:31
  • 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 Nov 06 '15 at 20:06
  • @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. – Tim Nov 25 '15 at 09:01
  • 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. – Tim Nov 25 '15 at 09:03
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.

Hugo
  • 4,004
  • 1
  • 31
  • 33
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).

Community
  • 1
  • 1
Dan Hunsaker
  • 309
  • 2
  • 6
  • 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 – jthill Jan 13 '13 at 07:59
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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-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.

msingleton
  • 115
  • 1
  • 2