Back-dating Git commits based on file modification dates#
I fell down a bit of a rabbit hole this morning. In trying to figure out where the idea of celebrating World Wide Web Day on August 1st came from I ran across Tim Berner-Lee’s original code for the WorldWideWeb application for NeXT on the W3C’s website:
It’s served up as a browseable Apache directory listing, but that’s not the most delightful way to browse code - clicking a link to a .m file triggers a download, for example.
I decided to copy the code over to a GitHub repository, in order to browse it more easily with syntax highlighting and the like.
Short version: to back-date Git commits you need to set the following environment variables before running git commit:
GIT_AUTHOR_DATE - to a format like 2004-01-25 00:00:00
GIT_COMMITTER_DATE - same
GIT_AUTHOR_NAME
GIT_AUTHOR_EMAIL
GIT_COMMITTER_NAME
GIT_COMMITTER_EMAIL
I figured this out through asking Claude to write me a script to rebuild my repo based on those last modified dates:
I want to create a GitHub repository where I back-date files to when they were first created
I have the files on my local disk like this:
1
-rw-r--r-- 1 simon wheel 1.5K Sep 4 1991 Anchor.h
2
-rw-r--r-- 1 simon wheel 8.1K Sep 4 1991 Anchor.m
3
-rw-r--r-- 1 simon wheel 3.6K Dec 1 1993 Bugs.html
Write me a Python script I can run which will group files by date and then do one grouped commit for each of those dates, backdated to the date (at 00:00), with an author that I pass to the script like this:
Problem: the script you wrote sets the commit date to now - I want the commit date to be backdated to the author date
Which got this v2 - almost right, but I made one more tweak:
Set the committer to the same as the author
And that produced a v3 which ran correctly, but made a weird editorial choice to use @example.com in the commit messages. I fixed that and ended up with this script - all comments are by me: