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 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
This question is related to
git
version-control
repository
git-commit
This is an old question but I recently stumbled upon it.
git commit --date='2021-01-01 12:12:00' -m "message"
worked properly and verified it on GitHub
.
The following is what I use to commit changes on foo
to N=1
days in the past:
git add foo
git commit -m "Update foo"
git commit --amend --date="$(date -v-1d)"
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:
git add foo ; git commit --date "yesterday" -m "Update"
You can always change a date on your computer, make a commit, then change the date back and push.
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
, then git 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
and cut
, but stat(1) is more direct
git commit --date="`stat -c %y myfile`" myfile
To make a commit that looks like it was done in the past you have to set both GIT_AUTHOR_DATE
and GIT_COMMITTER_DATE
:
GIT_AUTHOR_DATE=$(date -d'...') GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" git commit -m '...'
where date -d'...'
can be exact date like 2019-01-01 12:00:00
or relative like 5 months ago 24 days ago
.
To see both dates in git log use:
git log --pretty=fuller
This also works for merge commits:
GIT_AUTHOR_DATE=$(date -d'...') GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE" git merge <branchname> --no-ff
In my case, while using the --date option, my git process crashed. May be I did something terrible. And as a result some index.lock file appeared. So I manually deleted the .lock files from .git folder and executed, for all modified files to be commited in passed dates and it worked this time. Thanx for all the answers here.
git commit --date="`date --date='2 day ago'`" -am "update"
Or just use a fake-git-history to generate it for a specific data range.
You can create the commit as usual, but when you commit, set the environment variables GIT_AUTHOR_DATE
and GIT_COMMITTER_DATE
to the appropriate datetimes.
Of course, this will make the commit at the tip of your branch (i.e., in front of the current HEAD commit). If you want to push it back farther in the repo, you have to get a bit fancy. Let's say you have this history:
o--o--o--o--o
And you want your new commit (marked as "X") to appear second:
o--X--o--o--o--o
The easiest way would be to branch from the first commit, add your new commit, then rebase all other commits on top of the new one. Like so:
$ git checkout -b new_commit $desired_parent_of_new_commit
$ git add new_file
$ GIT_AUTHOR_DATE='your date' GIT_COMMITTER_DATE='your date' git commit -m 'new (old) files'
$ git checkout master
$ git rebase new_commit
$ git branch -d new_commit
I know this question is quite old, but that's what actually worked for me:
git commit --date="10 day ago" -m "Your commit message"
Source: Stackoverflow.com