I'm converting everything over to Git for my own personal use and I found some old versions of a file already in the repository. How do I commit it to the history in the correct order according the the file's "date modified" so I have an accurate history of the file?
I was told something like this would work:
git filter-branch --env-filter="GIT_AUTHOR_DATE=... --index-filter "git commit path/to/file --date " --tag-name-filter cat -- --all
In my case over time I had saved a bunch of versions of myfile as myfile_bak, myfile_old, myfile_2010, backups/myfile etc. I wanted to put myfile's history in git using their modification dates. So rename the oldest to myfile,
git add myfile
, thengit commit --date=(modification date from ls -l) myfile
, rename next oldest to myfile, another git commit with --date, repeat...To automate this somewhat, you can use shell-foo to get the modification time of the file. I started with
ls -l
andcut
, but stat(1) is more directThe following is what I use to commit changes on
foo
toN=1
days in the past:If you want to commit to a even older date, say 3 days back, just change the
date
argument:date -v-3d
.That's really useful when you forget to commit something yesterday, for instance.
UPDATE:
--date
also accepts expressions like--date "3 days ago"
or even--date "yesterday"
. So we can reduce it to one line command:To make a commit that looks like it was done in the past you have to set both
GIT_AUTHOR_DATE
andGIT_COMMITTER_DATE
:where
date -d'...'
can be exact date like2019-01-01 12:00:00
or relative like5 months ago 24 days ago
.To see both dates in git log use:
This also works for merge commits:
You can always change a date on your computer, make a commit, then change the date back and push.
I know this question is quite old, but that's what actually worked for me:
Or just use a fake-git-history to generate it for a specific data range.