How can one change the timestamp of an old commit in Git?
Solution 1:
You can do an interactive rebase and choose edit
for the commit whose date you would like to alter. When the rebase process stops for amending the commit you type in for instance:
git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit
P.S. --date=now
will use the current time.
Afterward, you continue your interactive rebase.
To change the commit date instead of the author date:
GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend --no-edit
The lines above set an environment variable GIT_COMMITTER_DATE
which is used in amending commit.
Everything is tested in Git Bash.
Solution 2:
Use git filter-branch
with an env filter that sets GIT_AUTHOR_DATE
and GIT_COMMITTER_DATE
for the specific hash of the commit you're looking to fix.
This will invalidate that and all future hashes.
Example:
If you wanted to change the dates of commit 119f9ecf58069b265ab22f1f97d2b648faf932e0
, you could do so with something like this:
git filter-branch --env-filter \
'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
then
export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
fi'
Solution 3:
A better way to handle all of these suggestions in one command is
LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
This will set the last commit's commit and author date to "right now."
Solution 4:
Just do git commit --amend --reset-author --no-edit
. For older commits, you can do an interactive rebase and choose edit
for the commit whose date you want to modify.
git rebase -i <ref>
Then amend the commit with --reset-author
and --no-edit
to change the author date to the current date:
git commit --amend --reset-author --no-edit
Finally continue with your interactive rebase:
git rebase --continue
Solution 5:
I wrote a script and Homebrew package for this. Super easy to install, you can find it on GitHub PotatoLabs/git-redate
page.
Syntax:
git redate -c 3
You just have to run git redate
and you'll be able to edit all the dates in vim of the most recent 5 commits (there's also a -c
option for how many commits you want to go back, it just defaults to 5). Let me know if you have any questions, comments, or suggestions!